不同的子类对象调用相同的父类方法,产生不同的执行结果
class Dog(object):
def __init__(self,name):
self.name=name
def game(self):
print("%s玩耍"%self.name)
class XiaoTianDog(Dog):
def game(self):
print("%s在天上完"%self.name)
class Person(object):
def __init__(self,name):
self.name=name
def game_with_dog(self,dog): #dog是定义的一个形参
print("%s和%s快乐玩耍"%(self.name,dog.name))
dog.game()
#wangcai=Dog("旺财")
wangcai=XiaoTianDog("feitian")
xiaoming=Person("小明")
xiaoming.game_with_dog(wangcai)#实参类传入
-----------------
D:\Anaconda\python.exe D:/Pycharm/PycharmProjects/python多态/k01_多态案例.py
小明和feitian快乐玩耍
feitian在天上完
Process finished with exit code 0