1 # -*- coding: utf-8 -*- 2 """ 3 Created on Mon Nov 12 15:05:20 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, miles): 14 Aricraft.mileages += miles 15 print("the aircrift has", Aricraft.mileages, "miles") 16 17 aricraft = Aricraft("涡扇9", "800km/h") 18 aricraft.fly(1000) 19 aricraft.fly(600) 20 21 class Fighter(Aricraft): # 继承 22 def __missile(self): # 私有方法,只能类内访问 23 print("emission missile !") 24 25 def fly(self, miles): 26 Aricraft.mileages += miles 27 print("the fighter has", Aricraft.mileages, "miles") 28 Fighter.__missile(self) 29 30 fighter = Fighter("涡扇15", "1200km/h") 31 fighter.fly(1800) 32 fighter.fly(1600) 33 # fighter.__missile() AttributeError: 'Fighter' object has no attribute '__missile' 34
结果: