super使用简介

def super(cls, inst): mro = inst.__class__.mro() return mro[mro.index(cls) + 1]
1.inst 代表MRO列表
2.定位当前类在MRO中的索引,返回索引+1的位置

class A:
    def hahaha(self):
    print('A')

class B(A):
    def hahaha(self):
    super().hahaha()
    #super(B,self).hahaha()
    #A.hahaha(self)
    print('B')

a = A()
b = B()
b.hahaha()  # 执行结果A,B
super(B,b).hahaha()  # 执行结果 A

猜你喜欢

转载自www.cnblogs.com/JackShi/p/12290412.html