面向对象--多继承的C3算法

Posted chenzhiming

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了面向对象--多继承的C3算法相关的知识,希望对你有一定的参考价值。

 在python3的面向对象中,最底层的父类都默认继承Object类,所有的类都是新式类,多继承的继承顺序遵循广度优先规则,也就是C3算法.

C3算法就是 计算继承顺序时,先把要计算的子类名拿出来,在加上继承的父类的继承顺序

 

从左往右找第一个继承的父类继承顺序,这样会出现3中情况:

  1.第一位出现的父类名,并且在后面没有出现的提出来

  2.第一位出现的父类在后面出现并且也都是在第一位,那也可以提出来

  3.第一位出现的父类在后面出现但是不在第一位,那么我们就要找第二个继承的父类继承顺序,看他的第一位的父类名是什么

 

咱们先看一个简单的了解一下

 

技术分享图片
1 class A:
2     pass
3 class B(A):
4     pass
5 class C(A):
6     pass
7 class D(B, C):
8     pass
简单的C3模型

 

技术分享图片

 

技术分享图片
 1 L(A) = [A] + [O]
 2    A = [O]
 3       = AO
 4 
 5 L(B) = [B] + [AO]
 6    B = [AO]
 7   BA = [O]
 8      =BAO
 9 
10 L(C) =[C]+[AO]
11    C = [AO]
12   CA = [O]
13      =CAO
14 
15 L(D) = [D] + [BAO] + [CAO]
16    D = [BAO] + [CAO]
17   DB = [AO] + [CAO]
18   DB = [AO] + [CAO]
19  DBC = [AO] + [AO]
20 DBCA = [O] + [O]
21      =DBCAO
推倒过程

 

 

 好了,大家是不是对C3算法有了一点了解了呢?像这道题,我们可以看出来,A是B和C 共同继承的父类,走到B这里,发现C的继承顺序中还有A,所以我们需要先继承C才能继承A.

准备好了吗?那么让我们来看一个复杂的吧

嘿嘿,就复杂一点点

技术分享图片
 1 class A:
 2     pass
 3 class B(A):
 4     pass
 5 class C(A):
 6     pass
 7 class D(B, C):
 8     pass
 9 class E(C, A):
10     pass
11 class F(D, E):
12     pass
13 class X:
14     pass
15 class Y:
16     pass
17 class M(X, Y):
18     pass
19 class G(E, M):
20     pass
21 class H(G, F):
22     pass
先看看题

为你们准备好了图片,先继承左边箭头指出的父类哦~

技术分享图片

 

技术分享图片
 1 L(A) = [A] + [O]
 2    A = [O]
 3       = AO
 4 
 5 L(X) = [X] + [O]
 6    X = [O]
 7      = XO
 8 
 9 L(Y) = [Y]+[O]
10    Y = [O]
11      = YO
12 
13 L(B) = [B] + [AO]
14    B = [AO]
15   BA = [O]
16      =BAO
17 
18 L(C) =[C]+[AO]
19    C = [AO]
20   CA = [O]
21      =CAO
22 
23 L(D) = [D] + [BAO] + [CAO]
24    D = [BAO] + [CAO]
25   DB = [AO] + [CAO]
26   DB = [AO] + [CAO]
27  DBC = [AO] + [AO]
28 DBCA = [O] + [O]
29      =DBCAO
30 
31 L(E)  = [E] + [CAO]+[AO]
32     E = [CAO]+[AO]
33    EC = [AO]+[AO]
34   ECA = [O]+[O]
35       =ECAO
36 
37 L(F)   = [F]+[DBCAO]+[ECAO]
38      F = [DBCAO]+[ECAO]
39     FD = [BCAO]+[ECAO]
40    FDB = [CAO]+[ECAO]
41   FDBE = [CAO]+[CAO]
42  FDBEC = [AO]+[AO]
43 FDBECA = [O]+[O]
44        =FDBECAO
45 
46 L(M) = [M] + [XO] + [YO]
47    M = [XO] + [YO]
48   MX = [O] + [YO]
49  MXY = [O] + [O]
50      = MXYO
51 
52 L(G)     = [G] + [ECAO] + [MXYO]
53       G = [ECAO] + [MXYO]
54      GE = [CAO] + [MXYO]
55     GEC = [AO] + [MXYO]
56    GECA = [O] + [MXYO]
57   GECAM = [O] + [XYO]
58  GECAMX = [O] + [YO]
59 GECAMXY = [O] + [O]
60         =GECAMXYO
61 
62 L(H)        = [H] + [GECAMXYO] + [FDBECAO]
63           H = [GECAMXYO] + [FDBECAO]
64          HG = [ECAMXYO] + [FDBECAO]
65         HGF = [ECAMXYO] + [DBECAO]
66        HGFD = [ECAMXYO] + [BECAO]
67       HGFDB = [ECAMXYO] + [ECAO]
68      HGFDBE = [CAMXYO] + [CAO]
69     HGFDBEC = [AMXYO] + [AO]
70    HGFDBECA = [MXYO] + [O]
71   HGFDBECAM = [XYO] + [O]
72  HGFDBECAMX = [YO] + [O]
73 HGFDBECAMXY = [O] + [O]
74             =HGFDBECAMXYO
推倒过程

 

是不是有点好看.....看着这么复杂,但是若是你会了C3算法,是不是觉得就是字多了点,一点难度都没有呢?

那么,,我来教大家一个看继承顺序的小窍门

技术分享图片
吃头疼片..







当然不是了,python为我们准备了__mro__方法
print(H.__mro__)就可以查看刚才那个有点复杂继承顺序了
小窍门

 

 

答案就是

[<class __main__.H>, <class __main__.G>, <class __main__.F>, <class __main__.D>, <class __main__.B>, <class __main__.E>, <class __main__.C>, <class __main__.A>, <class __main__.M>, <class __main__.X>, <class __main__.Y>, <class object>]

 

是不是和咱们计算的一样呢?嘿嘿

 

以上是关于面向对象--多继承的C3算法的主要内容,如果未能解决你的问题,请参考以下文章

python小兵 面向对象继承super和c3算法

多继承补充

继承c3,网络编程,相互通信

面向对象之多重继承

面向对象进阶

python多继承之C3算法