• super


    # class Base1:
    #     def chi(self):
    #         print("我是可怜的Base1")
    #
    # class Base2:
    #     def chi(self):
    #         print("我是可怜的Base2")
    #
    # class Base3:
    #     def chi(self):
    #         print("我是可怜的Base3")
    #
    # class Bar(Base1, Base2, Base3):
    #     def chi(self):
    #         print("我是Bar里面的吃1")
    #         # super(类名, self) 从某个类开始找下一个MRO
    #         super(Base2, self).chi() # 此时调用的super. 在Bar调用 -> super表示找MRO里的下一个
    #         # super().chi() # super(Bar, self).chi()
    #         print("我是Bar里面的吃2")
    #
    # b = Bar() # Bar, Base1, Base2, Base3, object
    # b.chi()
    # print(Bar.__mro__)
    
    
    # class Base1:
    #     def chi(self):
    #         super().chi() # super找的是MRO的下一个.
    #         print("我是可怜的Base1")
    #
    # class Base2:
    #     def chi(self):
    #         super().chi()
    #         print("我是可怜的Base2")
    #
    # class Base3:
    #     def chi(self):
    #         print("我是可怜的Base3")
    #
    # class Bar(Base1, Base2, Base3):
    #     def chi(self):
    #         print("我是Bar里面的吃1")
    #         # super(类名, self) 从某个类开始找下一个MRO
    #         super(Bar, self).chi() # 此时调用的super. 在Bar调用 -> super表示找MRO里的下一个
    #         # super().chi() # super(Bar, self).chi()
    #         print("我是Bar里面的吃2")
    #
    # b = Bar() # 报错 Base1 object
    # b.chi() # 报错
    # # 坑
    
    
    # MRO + super ⾯试题
    class Init(object):
        def __init__(self, v): # 2
            print("init")
            self.val = v
    
    class Add2(Init):
        def __init__(self, val): # 2
            print("Add2")
            super(Add2, self).__init__(val) # Mult
            print(self.val)
            self.val += 2
    
    class Mult(Init):
        def __init__(self, val):
            print("Mult")
            super(Mult, self).__init__(val) # Haha
            self.val *= 5
    
    class HaHa(Init):
       def __init__(self, val):
            print("哈哈")
            super(HaHa, self).__init__(val) # Init
            self.val /= 5
    
    class Pro(Add2,Mult,HaHa): #
        pass
    
    class Incr(Pro): # incr->pro->add2->Mult->HaHa->init
        def __init__(self, val): # 5
            super(Incr, self).__init__(val) # Add2
            self.val += 1
    
    
    
    '''
    MRO: method resolution order
    Incr,Pro,Add2,Mult,HaHa,Init,Object
    
    super()
    a = Incr()
    a.chi()
    '''
    
    # # print(Incr.__mro__)
    # # Incr Pro Add2 Mult HaHa Init
    # p = Incr(5) # p.val = 8.0
    # # Add2
    # # Mult
    # # 哈哈
    # # init
    # # 5.0
    # # 8.0
    # print(p.val)
    #
    c = Add2(2) # Add2, Init, Object c.val = 2
    # Add2
    # init
    # 2
    # 4
    
    print(c.val) # 4
    #
    

      

  • 相关阅读:
    css3360度旋转动画
    PAT 1084 外观数列(20)(代码+思路+推荐测试用例)
    PAT 1083 是否存在相等的差(20)(代码+思路)
    PAT 1082 射击比赛(20)(代码+思路)
    PAT 1079 延迟的回文数(代码+思路)
    PAT 1078 字符串压缩与解压(20)(代码+思路)
    PAT 1077 互评成绩计算(20)(代码+思路)
    PAT 1073 多选题常见计分法(20)(代码+思路)
    PAT 1072 开学寄语(20)(代码+思路)
    PAT 1069 微博转发抽奖(20)(代码+思路+测试点4)
  • 原文地址:https://www.cnblogs.com/work14/p/10187583.html
Copyright © 2020-2023  润新知