python 多态和鸭子类型
class Animal():
def say(self):
print('动物在叫')
class People(Animal):
def say(self):
super().say()
print('嘤嘤嘤')
class Dog(Animal):
def say(self):
super().say()
print('汪汪汪')
class Pig(Animal):
def say(self):
super().say()
print('哼哼哼')
a=People()
b=Dog()
c=Pig()
def animal_say(animal):
animal.say()
animal_say(a)
animal_say(b)
animal_say(c)
class Cpu():
def read(self):
print('cpu read')
def write(self):
print('cpu write')
class Mem():
def read(self):
print('mem read')
def write(self):
print('mem write')
class Txt():
def read(self):
print('txt read')
def write(self):
print('txt write')
obj1=Cpu()
obj2=Mem()
obj3=Txt()
def read_file(obj):
obj.read()
read_file(obj1)
read_file(obj2)
read_file(obj3)