python 多态
1. 多态:同一种事物的多种形态
import abc class Animal(metaclass=abc.ABCMeta): @abc.abstractmethod def speak(self): pass class Pig(Animal): def speak(self): print('哼哼') class Dog(Animal): def speak(self): print('汪汪') class People(Animal): def speak(self): print('say hello') people1=People() dog1=Dog() pig1=Pig() # 不推荐使用这种方式,这种方式需要所有类继承父类
#推荐方式 class Disk: def read(self): print('disk read') def write(self): print('disk write') class Process: def read(self): print('Process read') def write(self): print('Process write') #python 崇尚鸭子类型,像就行
2. 多态性:指的是在不考虑对象具体类型的情况下,直接使用对象(对象的方法)
people1.speak() dog1.speak() pig1.speak() def talk(obj): obj.speak() talk(people1) #people1.speak() talk(dog1) talk(pig1)