Python类继承,方法重写及私有方法

 # -*- coding: utf-8 -*-
"""
Created on Mon Nov 12 15:05:20 2018 @author: zhen
""" class Aricraft:
mileages = 0.0 # 类变量,在该类及其子类的实例*享
def __init__(self, engine, speed):
self.engine = engine
self.speed = speed
def fly(self, miles):
Aricraft.mileages += miles
print("the aircrift has", Aricraft.mileages, "miles") aricraft = Aricraft("涡扇9", "800km/h")
aricraft.fly(1000)
aricraft.fly(600) class Fighter(Aricraft): # 继承
def __missile(self): # 私有方法,只能类内访问
print("emission missile !") def fly(self, miles):
Aricraft.mileages += miles
print("the fighter has", Aricraft.mileages, "miles")
Fighter.__missile(self) fighter = Fighter("涡扇15", "1200km/h")
fighter.fly(1800)
fighter.fly(1600)
# fighter.__missile() AttributeError: 'Fighter' object has no attribute '__missile'

结果:

Python类继承,方法重写及私有方法

上一篇:lxml包引入错误


下一篇:springboot+kotlin+springcloud+java+grade+maven混编?