多重继承用法

    技术2022-07-11  79

    多重继承时,为避免父类方法被重复调用,可使用super类,用法如下:

    #单一继承时用父类名调用,多重继承时用super class A(object): def test(self): print("A's test") class B(A): def test(self): print("B's test") super(B,self).test() #避免父类方法重复调用,super是一个类不是函数 class C(A): def test(self): print("C's test") super(C,self).test() class D(B,C): def test(self): print("D's test") super(D,self).test() if __name__=="__main__": d = D() d.test() print(D.mro()) #查看类的继承顺序

    运行结果:

    Processed: 0.017, SQL: 12