python之多继承与__mro__的使用


1
class Base(object): 2 def text(self): 3 print('------text-----') 4 class A(Base): 5 def text(self): 6 print('------text1-----') 7 class B(Base): 8 def text(self): 9 print('------text2-----') 10 class C(A,B): 11 def text(self): 12 print('------text3-----') 13 14 c = C() 15 c.text()

多继承:使用方法类似于单继承,概念上是说一个子类包含了多个父类。

在上述代码中,当我们调用C这个类中的方法时,由于它继承的所有父类都有相同的方法名,这个时候我们可以通过打印print(类名.__mro__)来查看系统调用方法的具体顺序是什么。

print(C.__mro__)

显示的结果如下:

表明了调用的优先顺序是:C>A>B>Base>object

猜你喜欢

转载自www.cnblogs.com/liyun-1213/p/8991652.html