**
python多态
**
class Animal:
def speak(self):
pass
class Dog(Animal):
def speak(self):
print("汪汪汪")
class Cat(Animal):
def speak(self):
print("喵喵喵")
def make_noise(animal: Animal):
animal.speak()
dog = Dog()
cat = Cat()
make_noise(dog)
make_noise(cat)
# 抽象类
class AC:
def cool_wind(self):
"""制冷"""
pass
def hot_wind(self):
"""制热"""
pass
def swing_l(self):
"""摆风"""
pass
class Midea_Ac(AC):
def cool_wind(self):
print("美的空调制冷")
def hot_wind(self):
print("美的制热")
def swing_l(self):
print("美的摆风")
class GREE_Ac(AC):
def cool_wind(self):
print("格力空调制冷")
def hot_wind(self):
print("格力制热")
def swing_l(self):
print("格力摆风")
def make_colls(ac:AC):
ac.cool_wind()
midea_ac=Midea_Ac()
gree_ac=GREE_Ac()
make_colls(midea_ac)
make_colls(gree_ac)
运行结果:
汪汪汪
喵喵喵
美的空调制冷
格力空调制冷