新式类、经典类与多继承
作者:互联网
python3所有的类都继承自object类
新式类:
1.python3中,所有类都是新式类
2.python2中主动继承object类
经典类:
python2中,不继承object类
# 多继承: class A:pass class B(A):pass # B继承A class C(A):pass # C继承A class D(B, C):pass # D同时继承B和C print(D.mro()) # 类的mro方法只在新式类中有,可以查看该类的继承顺序 # 新式类:广度优先 可以直接使用类名.mro()方法查看继承顺序 # 当既可以走广度又可以走深度时,总是广度再深度 # d = D() # d.属性或方法时,本身没有则从父类中找,顺序为D-->B-->C-->A # 经典类:总是深度优先 # d = D() # d.属性或方法时,本身没有则从父类中找,顺序为D-->B-->A-->C """ C3算法:存在于新式类中,可以通过类.mro()方法查看继承顺序的结果 如果是单继承,那么总是按照子类-->父类的顺序来计算
例如: A(O) = [AO] B(A) = [BAO] C(A) = [CAO] D(B) = [DBAO] E(C) = [ECAO] 如果是多继承,那么需要按照自己本类,父类1的继承顺序,父类2的继承顺序.... F(D,E) = merge(D(B) + E(C)) = [F] + D(B) + E(C) = [F] + [DBAO] + [ECAO] = FD + [BAO] + [ECAO] = FDB + [AO] + [ECAO] = FDBE + [AO] + [CAO] = FDBEC + [AO] + [AO] = FDBECA + [O] + [O] = FDBECAO """
标签:顺序,继承,AO,--,经典,父类,新式 来源: https://www.cnblogs.com/GOD-L/p/13537406.html