Day_13_PM_Polymorphism
# 封装,继承,多态(了解)
# 多态: 在继承的基础上, 多个子类重写父类的一个方法, 在不同的子类中写不同的功能, 那么
# 用父类的对象指向不同的子类的对象, 调用该方法, 则可以实现不同的功能
# 父类
class Animal:
def eat(self):
pass
# 子类
class Dog(Animal):
def eat(self):
print('吃骨头')
class Cat(Animal):
def eat(self):
print('吃鱼')
class Cow(Animal):
def eat(self):
print('吃草')
#
class Person:
def __init__(self, name):
self.name = name
def keep(self, animal):
print(f'{self.name}养了一只小动物')
animal.eat()
# 创建对象
dog = Dog()
cat = Cat()
cow = Cow()
p = Person('小明')
p.keep(dog)
# 弱类型
# a = 10
# a = 'hello'
# 强类型
# int a = 10
# a = 'hello # 报错