Discuss / Python / 求解????

求解????

Topic source

楠先森咩

#1 Created at ... [Delete] [Delete and Lock User]

这里给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'


楠先森咩

#3 Created at ... [Delete] [Delete and Lock User]

明白了 谢谢!!!!


  • 1

Reply