1 # -*- coding: utf-8 -*- 2 """ 3 Created on Thu Nov 15 10:34:25 2018 4 5 @author: zhen 6 """ 7 8 class Aricraft: 9 mileages = 0.0 # 类变量,在该类及其子类的实例中共享 10 def __init__(self, engine, speed): 11 self.engine = engine 12 self.speed = speed 13 def fly(self): 14 Aricraft.mileages += float(self.speed) 15 print("the aircrift has", Aricraft.mileages, "miles") 16 17 class Fighter(Aricraft): # 继承 18 def __missile(self): # 私有方法,只能类内访问 19 print("emission missile !") 20 21 def fly(self): 22 Aricraft.mileages += float(self.speed) 23 print("the fighter has", Aricraft.mileages, "miles") 24 Fighter.__missile(self) 25 26 def indication(obj): # 多态,动态执行 27 obj.fly() 28 29 aricraft = Aricraft("涡扇10", "800") 30 fighter = Fighter("涡扇15", "1200") 31 32 indication(aricraft) 33 indication(fighter)
结果: