day 21继承

1.了解Python2和python3类的区别:
  python2.3之前使用的是经典类, 在2.3版本之后组,使用的是新式类
MRO: method resolution order  方法的查找顺序
MRO: method resolution order 方法的查找顺序
 
        class Base:
            pass
 
        class Base1:
            def chi():
                pass
 
        class Bar(Base, Base1):
            pass
 
        b = Bar() # Bar -> Base -> Base1
        b.chi()
2.经典类的MRO  树形结构的深度优先遍历   -> 树形结构遍历
注意:新式类的不是广度的遍历
3.新式类的MRO  c3算法(重点,难点)
    1.拆分
     2.合并
用头和身体进行比较,如果头在身子里面存在,取身子的头,如果头在身子里面不存在,取头
class A:
    pass
class B(A):
    pass
class C(A):
    pass
class D(B, C):
    pass
class E(C, A):
    pass
class F(D, E):
    pass
class M:
    pass
class N(M):
    pass
class P(E, A):
    pass
class X:
    pass
class Q(P,N,X):
    pass
class G(Q, F):
    pass
class H(G, F):
    pass
 
'''
L(H) = H + L(G) + L(F) + GF # HGQPFDBECANMX
L(G) = G + L(Q) + L(F) + QF # GQPFDBECANMX
L(Q) = Q + L(P) + L(N) + X + PNX # QPECANMX
L(P) = P + L(E) + A + EA  # PECA
L(E) = E + L(C) + A + CA   # ECA
L(C) = C + A + A   # CA
L(N) = N + M # NM
 
L(F) = F + L(D) + L(E) + DE # FDBECA
L(D) = D + L(B) + L(C) + BC # DBCA
 
 
'''
 
'''
    求H的MRO
    设求MRO的算法是L
    L(H) = H + L(G) + L(F) + GF
    L(G) = G + L(E) + E
    L(E) = E + L(C) + L(A) + CA
    L(C) = C + L(A) + A
    L(A) = A
    L(F) = F + L(D) + L(E) + DE
    L(D) = D + L(B) + L(C) + BC
    L(B) = B + L(A) + A
 
    # 加法:merge(), 拿第一项的第一位和 后面每项的除了第一位比较. 如果没有出现, 则该位元素算出
        如果出现了. 此时开始下一项的第一位继续和后面每一项的除了第一位比较:
        
        用头和后面身体比较
            
    L(H) = H + L(G) + L(F) + GF # ECA + DBECA  = HGFDBECAO
    L(G) = G + L(E) + E # GECA
    L(E) = E + L(C) + L(A) + CA # ECA
    L(C) = C + L(A) + A  # CA
    L(A) = A
    L(F) = F + L(D) + L(E) + DE # FDBECA
    L(D) = D + L(B) + L(C) + BC # DBCA
    L(B) = B + A + A   # BA
'''
print(H.__mro__)
4.super() 找MRO顺序的下一个
class Base1:
    def chi(self):
        print("我是可怜的Base1")
 
class Base2:
    def chi(self):
        print("我是可怜的Base2")
 
class Base3:
    def chi(self):
        print("我是可怜的Base3")
 
class Bar(Base1, Base2, Base3):
    def chi(self):
        print("我是Bar里面的吃1")
        # super(类名, self) 从某个类开始找下一个MRO
        super(Base2, self).chi() # 此时调用的super. 在Bar调用 -> super表示找MRO里的下一个
        # super().chi() # super(Bar, self).chi()
        print("我是Bar里面的吃2")
 
b = Bar() # Bar, Base1, Base2, Base3, object   顺序
b.chi()
print(Bar.__mro__)
5.一道面试题
# MRO + super ⾯试题
class Init(object):
    def __init__(self, v): # 2
        print("init")
        self.val = v
 
class Add2(Init):
    def __init__(self, val): # 2
        print("Add2")
        super(Add2, self).__init__(val) # Mult
        print(self.val)
        self.val += 2
 
class Mult(Init):
    def __init__(self, val):
        print("Mult")
        super(Mult, self).__init__(val) # Haha
        self.val *= 5
 
class HaHa(Init):
   def __init__(self, val):
        print("哈哈")
        super(HaHa, self).__init__(val) # Init
        self.val /= 5
 
class Pro(Add2,Mult,HaHa): #
    pass
 
class Incr(Pro): # incr->pro->add2->Mult->HaHa->init
    def __init__(self, val): # 5
        super(Incr, self).__init__(val) # Add2
        self.val += 1
 
 
 
'''
MRO: method resolution order
Incr,Pro,Add2,Mult,HaHa,Init,Object
 
super()
a = Incr()
a.chi()
'''
 
# print(Incr.__mro__)
# Incr Pro Add2 Mult HaHa Init
p = Incr(5) # p.val = 8.0
# Add2
# Mult
# 哈哈
# init
# 5.0
# 8.0
print(p.val)
 
c = Add2(2) # Add2, Init, Object c.val = 2
# Add2
# init
# 2
# 4
 
print(c.val) # 4
 
posted @ 2018-12-24 17:29  杨洪涛  阅读(115)  评论(0编辑  收藏  举报