写一个父类动物类,有吃和睡两个方法,在写一个子类老虎继承父类,然后在这一个子类兔子继承父类,最后用接口把两个子类结合起来
时间: 2024-02-01 13:15:16 浏览: 61
```python
class Animal:
def eat(self):
print("Animal is eating...")
def sleep(self):
print("Animal is sleeping...")
class Tiger(Animal):
def eat(self):
print("Tiger is eating meat...")
class Rabbit(Animal):
def eat(self):
print("Rabbit is eating carrot...")
class AnimalInterface:
def __init__(self, animal):
self.animal = animal
def eat(self):
self.animal.eat()
def sleep(self):
self.animal.sleep()
tiger = Tiger()
rabbit = Rabbit()
tiger_interface = AnimalInterface(tiger)
rabbit_interface = AnimalInterface(rabbit)
tiger_interface.eat() # Tiger is eating meat...
tiger_interface.sleep() # Animal is sleeping...
rabbit_interface.eat() # Rabbit is eating carrot...
rabbit_interface.sleep() # Animal is sleeping...
```
阅读全文