我正在尝试学习oop概念,并且选择了python。据我所知,self.count和employee.count都调用类变量计数,它们都应该具有相同的值。但是,下面的代码,我看到self.count是1和employee.count为0python中的self.variable name和classname.variable之间的区别
class employee:
count=0
def __init__(self,x):
self.x=x
self.count=self.count+1
print ("this method is executed")
print (self.count)
print (employee.count)
emp1=employee("John")
所以他们显然是*不是*相同的变量;那么,你的问题是什么?你有没有尝试制造其他的“雇员”对象? –
No ... self.count和employee.count都指的是前面声明的同一个类变量count – vijay
@vijay但是直到你声明实例变量'self.count' ... – Keiwan