class Employee(object):
empCount=0
def __init__(self,name,salary):
self.name=name
self.salary=salary
print("__new__")
Employee.empCount+=1
def __del__(self):
print("__del__")
Employee.empCount-=1
def displayCount(self):
print("Total Employee %d" %Employee.empCount)
def displayEmployee(self):
print("name:"+self.name+", Salary:"+self.salary)
ep1=Employee("zhangsan","5K")
ep1.displayCount()
ep2=Employee("lisi","4K")
ep2.displayCount()
del ep1
print("Total Employee ",Employee.empCount)
empCount是一个静态方法,用于计数,每创建一个实例调用构造器方法empCount+1,每销毁一个实例调用解构器方法empCount-1
使用Windows命令台运行结果如下
但是在spyder编辑器中运行结果不一致
估计是spyder的内存处理机制的问题。结果应该是Windows的结果为正常的