class Dog:
dogbook
= {'黄色':30, '黑色':20, '白色':0}
def __init__(self
, name
, color
, weight
):
self
.name
= name
self
.color
= color
self
.weight
= weight
def bark(self
):
print(f
'{self.name} 叫了起来')
@
classmethod
def dog_num(cls
):
num
= 0
for v
in cls
.dogbook
.values
():
num
= num
+ v
return num
@
staticmethod
def total_weights(dogs
):
total
= 0
for o
in dogs
:
total
= total
+ o
.weight
return total
print(f
'共有 {Dog.dog_num()} 条狗')
d1
= Dog
('大黄', '黄色', 10)
d1
.bark
()
print(f
'共有 {d1.dog_num()} 条狗')
d2
= Dog
('旺财', '黑色', 8)
d2
.bark
()
print(f
'狗共重 {Dog.total_weights([d1, d2])} 公斤')
视频讲解:视频
转载请注明原文地址:https://blackberry.8miu.com/read-29344.html