当我运行它时,我得到了与创建的物种对象相对应的所需字符串。我的目标是能够在 Animal 类中创建一个名为 speak 的方法来实现相同的目的。但我对如何做到这一点没有很好的理解。
class Animal(object):
def __init__(self):
pass
def speak():
pass
class Mammal(Animal):
def __init__(self):
Animal.__init__(self)
class Cat(Mammal):
def __init__(self):
Mammal.__init__(self)
def __str__(self):
return "meeeow"
class Dog(Mammal):
def __init__(self):
Mammal.__init__(self)
def __str__(self):
return "wooof"
class Primate(Mammal):
def __init__(self):
Mammal.__init__(self)
class Hacker(Primate):
def __init__(self):
Primate.__init__(self)
def __str__(self):
return "Hello world!"
garfield = Cat()
print(garfield)
spike = Dog()
print(spike)
john = Hacker()
print(john)
慕容708150
四季花海
相关分类