可以使用点号 . 来访问对象的属性。使用如下类的名称访问类变量:
emp1.displayEmployee()
emp2.displayEmployee()
print "Total Employee %d" % Employee.empCount
实例
#!/usr/bin/python # -*- coding: UTF-8 -*- class Employee: '' empCount = 0 def __init__(self, name, salary): self.name = name self.salary = salary Employee.empCount += 1 def displayCount(self): print "Total Employee %d" % Employee.empCount def displayEmployee(self): print "Name : ", self.name, ", Salary: ", self.salary "" emp1 = Employee(") "" emp2 = Employee(") emp1.displayEmployee() emp2.displayEmployee() print "Total Employee %d" % Employee.empCount