这里给Student定义了一个name类属性。
class Student(object): name = 'Student'
可以给Student类增加一个类属性,每创建一个实例,该属性自动增加。
class Student(object): count = 0 def __init__(self, name): self.name = name Student.count=Student.count+1
为什么加这个count类属性的时候没有写成count="Student"?上下两个例子增加类属性有什么区别?
name = 'Student' 中的Student 和 class Student(object):中的Student没有关系啊,他只是为了给name 赋一个值,你可以换成name=123,name='xiaoming'
明白了 谢谢!!!!
Sign in to make a reply
楠先森咩
这里给Student定义了一个name类属性。
可以给Student类增加一个类属性,每创建一个实例,该属性自动增加。
为什么加这个count类属性的时候没有写成count="Student"?上下两个例子增加类属性有什么区别?