zoukankan      html  css  js  c++  java
  • Python多态

     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)

    结果:

  • 相关阅读:
    [usaco]Cow Pedigrees
    组合数取模
    [usaco]Controlling Companies
    ubuntu g++ 升级
    膜拜
    Node.js权威指南 (2)
    Vue.js 开发环境的搭建
    src路径问题:./ 与 ../
    vscode vue代码提示错误
    H5 localStorage sessionStorage
  • 原文地址:https://www.cnblogs.com/yszd/p/9962289.html
Copyright © 2011-2022 走看看