面向对象之补充 (*****)

1.经典类

  在python2的版本中·所有的类都是经典类  

2.新式类

  在python3的版本中所有类都是新式类(所有继承(object)的类都是新式类)

深度优先级(在经典类中都是深度优先)

class P1:
    def foo(self):
        print 'p1-foo'
 
 
class P2:
    def foo(self):
        print 'p2-foo'
 
    def bar(self):
        print 'p2-bar'
 
 
class C1(P1, P2):
    pass
 
 
class C2(P1, P2):
    def bar(self):
        print 'C2-bar'
 
 
class D(C1, C2):
    pass
 
 
d = D()
d.foo()  # 输出 p1-foo
d.bar()

  d.foo()继承顺序为(D —>C1—>P1)

  d.bar()继承顺序为(D—>C1 —>P1—>P2)

广度优先(在新式类中都是广度优先)

class P1(object):
    def foo(self):
        print 'p1-foo'
 
 
class P2(object):
    def foo(self):
        print 'p2-foo'
 
    def bar(self):
        print 'p2-bar'
 
 
class C1(P1, P2):
    pass
 
 
class C2(P1, P2):
    def bar(self):
        print 'C2-bar'
 
 
class D(C1, C2):


d = D()
d.foo()  # 输出 p1-foo
d.bar()  # 输出 c2-bar



d.foo()继承顺序:  C1——》C2——》P1
d.bar()继承顺序: C1——》C2

猜你喜欢

转载自www.cnblogs.com/wm0217/p/11767358.html