# 继承和多态
class Animal:
def eat(self):
print("Animal is eating")
class Bird(Animal): # 继承Animal类
def sing(self):
print("Bird is singing")
class Dog(Animal):
def eat(self): # 该类中自己有一个eat()方法
print("Dog is eating")
a = Animal() # 创建一个Animal对象
a.eat() # Animal is eating
b = Bird() # 创建一个Bird对象,继承Animal的eat()方法
b.eat() # Animal is eating
b.sing() # Bird is singing
c = Dog() # 创建一个Dog对象,重载eat()方法
c.eat() # Dog is eating 该类调用自己的eat()方法,python的多态
def demo_eat(x):
x.eat()
for item in [a, b, c]:
demo_eat(item) # Animal is eating / Animal is eating / Dog is eating