类里面只能定义属性和方法?
class Person(object): count=0 def __init__(self,name): print '\n',Person.count Person.count+=1 print Person.count self.name=name print Person.count p1 = Person('Bob') print Person.count p2 = Person('Alice') print Person.count print p2.count p3 = Person('Tim') print Person.count
如上、在类里面调用报错、但在构造函数中却不会、求大神解释、不甚感激、、还是说类中只能定义属性和方法?
class Person(object): count=0 def __init__(self,name): #print '\n',Person.count Person.count+=1 #print Person.count self.name=name def get_count(self): print Person.count p1 = Person('Bob') print Person.count print p1.get_count()
像这样就可以了、