编写一个学生类,产生一堆学生对象 要求:有一个计数器(属性),统计总共实例了多少个对象
class Student():
count = 0
def init(self,name):
self.name = name
Student.count += 1
a = Student(‘A’)
b = Student(‘B’)
c = Student(‘C’)
e = Student(‘D’)
print (Student.count)

这篇博客展示了如何在Python中定义一个学生类,包含一个静态计数器来记录实例化的对象数量。通过创建多个学生对象,计数器会自动增加,最后打印出总的学生对象数。这是一个简单的类实例化和计数的概念应用。
编写一个学生类,产生一堆学生对象 要求:有一个计数器(属性),统计总共实例了多少个对象
class Student():
count = 0
def init(self,name):
self.name = name
Student.count += 1
a = Student(‘A’)
b = Student(‘B’)
c = Student(‘C’)
e = Student(‘D’)
print (Student.count)


被折叠的 条评论
为什么被折叠?