20044031
2021-01-22 19:45
calss Anmial(object):
count = 0
def_init_(self,name,age):
self.name = name
self.age = age
Animal.count += 1
dog = Animal('wangwang',1)
print(Animal.count)
cat = Animal('mimi',3)
print(Animal.count)
pig = Animal('panpan',1)
print(Animal.count)
Animal.count += 1 改成 Animal.count =Animal.count + 1
init的前后是两个_ ,不是一个_
class Anmial(object):
count = 0
def __init__(self,name,age):
self.name=name
self.age=age
Anmial.count+=1
dog = Anmial('wangwang',1)
print(Anmial.count)
cat = Anmial('mimi',3)
print(Anmial.count)
pig = Anmial('panpan',1)
print(Anmial.count)
class打错了 Anmial 打成Animal 能运行就怪了
Python3 进阶教程(新版)
41910 学习 · 236 问题
相似问题