zoukankan      html  css  js  c++  java
  • super

      1 1、不正确用法
      2 class Vehicle:  # 定义交通工具类
      3     Country = 'China'
      4 
      5     def __init__(self, name, speed, load, power):
      6         self.name = name
      7         self.speed = speed
      8         self.load = load
      9         self.power = power
     10 
     11     def run(self):
     12         print('开动啦...')
     13 
     14 
     15 class Subway(Vehicle):  # 地铁
     16     def __init__(self, name, speed, load, power, line):
     17         Vehicle.__init__(self, name, speed, load, power)
     18         self.line = line
     19 
     20     def run(self):
     21         print('地铁%s号线欢迎您' % self.line)
     22         Vehicle.run(self)
     23 
     24 
     25 line13 = Subway('中国地铁', '180m/s', '1000人/箱', '', 13)
     26 line13.run()
     27 
     28 # 地铁13号线欢迎您
     29 # 开动啦...
     30 
     31 2、正确用法
     32 
     33 class Vehicle:  # 定义交通工具类
     34     Country = 'China'
     35 
     36     def __init__(self, name, speed, load, power):
     37         self.name = name
     38         self.speed = speed
     39         self.load = load
     40         self.power = power
     41 
     42     def run(self):
     43         print('开动啦...')
     44 
     45 
     46 class Subway(Vehicle):  # 地铁
     47     def __init__(self, name, speed, load, power, line):
     48         # super(Subway,self) 就相当于实例本身 在python3中super()等同于super(Subway,self)
     49         super().__init__(name, speed, load, power)
     50         self.line = line
     51 
     52     def run(self):
     53         print('地铁%s号线欢迎您' % self.line)
     54         print(super(Subway,self).run)
     55         super(Subway, self).run()
     56 
     57 
     58 class Mobike(Vehicle):  # 摩拜单车
     59     pass
     60 
     61 
     62 line13 = Subway('中国地铁', '180m/s', '1000人/箱', '', 13)
     63 line13.run()
     64 
     65 # 地铁13号线欢迎您
     66 # <bound method Vehicle.run of <__main__.Subway object at 0x00000000027DA390>>
     67 # 开动啦...
     68 
     69 
     70 惨案!!!!!!!!!!!!!!!!!!!!!!!
     71 
     72 #每个类中都继承了且重写了父类的方法
     73 class A:
     74     def __init__(self):
     75         print('A的构造方法')
     76 class B(A):
     77     def __init__(self):
     78         print('B的构造方法')
     79         A.__init__(self)
     80 
     81 
     82 class C(A):
     83     def __init__(self):
     84         print('C的构造方法')
     85         A.__init__(self)
     86 
     87 
     88 class D(B,C):
     89     def __init__(self):
     90         print('D的构造方法')
     91         B.__init__(self)
     92         C.__init__(self)
     93 
     94     pass
     95 f1=D()
     96 
     97 print(D.__mro__) #python2中没有这个属性
     98 
     99 # D的构造方法
    100 # B的构造方法
    101 # A的构造方法
    102 # C的构造方法
    103 # A的构造方法
    104 # (<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>)
    105 
    106 
    107 #每个类中都继承了且重写了父类的方法
    108 class A:
    109     def __init__(self):
    110         print('A的构造方法')
    111 class B(A):
    112     def __init__(self):
    113         print('B的构造方法')
    114         super(B,self).__init__()
    115 
    116 
    117 class C(A):
    118     def __init__(self):
    119         print('C的构造方法')
    120         super(C,self).__init__()
    121 
    122 
    123 class D(B,C):
    124     def __init__(self):
    125         print('D的构造方法')
    126         super(D,self).__init__()
    127 
    128 f1=D()
    129 
    130 print(D.__mro__) #python2中没有这个属性
    131 
    132 # D的构造方法
    133 # B的构造方法
    134 # C的构造方法
    135 # A的构造方法
    136 # (<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>)
    137 
    138 
    139 # 当你使用super()函数时,Python会在MRO列表上继续搜索下一个类。
        只要每个重定义的方法统一使用super()并只调用它一次,那么控制流最终会遍历完整个 140 # MRO列表,每个方法也只会被调用一次(注意注意注意:使用super调用的所有属性,
        都是从MRO列表当前的位置往后找,千万不要通过看代码去找继承关系,一 141 # 定要看MRO列表)

    可见  super完全按照__mro__的顺序来执行。
  • 相关阅读:
    给X轴添加滚动条,放大X轴Y轴
    win7下发布网站
    asp.net播放声音
    用Flash方式动态生成图表
    debian programming guid
    php 自动跳转的3种方法
    用expect实现su c功能,身份切换
    11 Top Opensource Resources for Cloud Computing
    转:Linux系统下Shell命令行快捷键实用技巧
    sed 替换
  • 原文地址:https://www.cnblogs.com/chedanlangren/p/7120733.html
Copyright © 2011-2022 走看看