码迷,mamicode.com
首页 > 编程语言 > 详细

Python:多继承时的继承顺序

时间:2019-07-09 16:43:17      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:一个   elf   pytho   多继承   广度   python3   int   深度优先   pass   

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

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的新式类中广度优先

 

Python:多继承时的继承顺序

标签:一个   elf   pytho   多继承   广度   python3   int   深度优先   pass   

原文地址:https://www.cnblogs.com/hancece/p/11158287.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!