多态
多态(以封装和继承为前提)
不同的子类对象调用相同的方法 产生不同的结果
Person():
work()
class Dog(object):
def __init__(self, name):
self.name = name
```
def game(self):
print('%s 开心的玩~' % (self.name))
```
class Gaofei(Dog):
# 父类方法不能满足子类的需求 重写game
```
def game(self):
print('%s和米老鼠一起玩~~~' %(self.name))
```
class Person(object):
def __init__(self,name):
self.name = name
```
def game_with_dog(self,dog):
print('%s和%s玩' %(self.name,dog.name))
```
# 创建一个狗对象
# wangcai = Dog('大黄')
wangcai = Gaofei('高飞')
# 创建一个人对象
xiaoming = Person('浩浩')
# 让小明和狗玩
xiaoming.game_with_dog(wangcai)