Python多继承
1 # -*- coding: utf-8 -*- 2 """ 3 Created on Tue Nov 13 16:56:03 2018 4 5 @author: zhen 6 """ 7 class Machine: 8 mileages = 0.0 # 类变量,在该类及其子类的实例中共享 9 def __init__(self, engine, speed): 10 self.engine = engine 11 self.speed = speed 12 13 class Aricraft(Machine): 14 def fly(self, miles): 15 Aricraft.mileages += miles 16 print("the aircrift has", Aricraft.mileages, "miles") 17 18 class Tank(Machine): 19 def travel(self, miles): 20 Tank.mileages += miles 21 print("the tank has", Tank.mileages, "miles") 22 23 aricraft = Aricraft("涡扇9", "800km/h") 24 aricraft.fly(600) 25 tank = Tank("V8缸涡轮增压", "120km/h") 26 tank.travel(300) 27 28 class Fighter(Aricraft,Tank): # 多继承,尽量避免多个父类存在相同函数的情况 29 def __missile(self): # 私有方法,只能类内访问 30 print("emission missile and shell !") 31 def fly(self, miles): 32 Aricraft.mileages += miles 33 print("the fighter has fly", Aricraft.mileages, "miles") 34 Fighter.__missile(self) 35 def travel(self, miles): 36 Tank.mileages += miles 37 print("the fighter has travel", Tank.mileages, "miles") 38 39 fighter = Fighter("涡扇15", "1200km/h") 40 fighter.fly(1800) 41 fighter.travel(600) 42 # 查看类继承顺序 43 print(Fighter.__mro__) # object是所有类的基类
结果: