如下代码实现了继承,请补全代码: class Animal: _total = 0 def __init__(self): ① ._total += 1 class Dog(Animal): __dtotal = 0 def __init__(self): super().__init__() ② .__dtotal += 1 @ ③ . def show(cls): print("animal: ", super()._total, ", dog:", cls.__dtotal) class Bird(Animal): __btotal = 0 def __init__(self): super().__init__() ④ .__btotal += 1 def show(self): print("animal:", super()._total, ", bird:", ⑤ .__btotal) d1 = Dog(); d2 = Dog(); b1 = Bird() d1.show(); b1.show()
时间: 2023-06-02 22:05:58 浏览: 118
答案如下:
class Animal:
_total = 0
def __init__(self):
Animal._total += 1
class Dog(Animal):
__dtotal = 0
def __init__(self):
super().__init__()
Dog.__dtotal = 1
@classmethod
def show(cls):
print("animal: ", super()._total, ", dog:", cls.__dtotal)
class Bird(Animal):
__btotal = 0
def __init__(self):
super().__init__()
Bird.__btotal = 1
def show(self):
print("animal:", super()._total, ", bird:", Bird.__btotal)
d1 = Dog()
d2 = Dog()
b1 = Bird()
d1.show()
b1.show()
阅读全文