多态:是指一类事物有多种形态(!!!!定义角度!!!!)
多态性:在继承的基础上, (!!!!使用角度!!!!!)
使用多态性,实现了利用函数统一调用一个接口
多态
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| class Animal: def run(self): raise AttributeError('子类必须实现这个方法') class People(Animal): def run(self): print('人正在走') class Pig(Animal): def run(self): print('pig is walking') class Dog(Animal): def run(self): print('dog is running') peo1=People() pig1=Pig() d1=Dog() peo1.run() pig1.run() d1.run()
|
多态性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| def func(obj): obj.run() func(peo1) func(pig1) func(d1) def func(obj): obj.run() func(peo1) func(pig1) func(d1)
|
即便是后期添加别的功能,调用都是一样的