Python:多继承时的继承顺序

当一个类继承了多个类时,继承顺序:

1.示例:

class A(object):#python2中的新式类,等同与python3中的class A:
    def say(self):
        print('A')


class B(A):
    # pass
    def say(self):
        print('B')


class C(A):
    pass
    # def say(self):
    #     print('c')

class D(C, B):
    pass
d=D()
d.say()   
print(D.mro())   #查看继承顺序
# [<class '__main__.D'>, <class '__main__.C'>, <class '__main__.B'>, <class '__main__.A'>, <class 'object'>]   #广度优先
#[<class '__main__.D'>, <class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>] 深度优先
#python 2 中深度优先
#python3或python2的新式类中广度优先

 

posted @ 2019-07-09 16:30  4jd121de2gf4e2sa5d  阅读(452)  评论(0编辑  收藏  举报