python百例

88-OOP之多重继承

2018-08-02  本文已影响4人  凯茜的老爸

类的父类(基类)可以有很多个,子类可以调用所有父类的方法。
如果有重名方法,生效的顺序是自下而上,自左而右。当然最好不要出现重名方法。

class A:
    def foo(self):
        print('in A foo')
    def hello(self):
        print('A hello')

class B:
    def bar(self):
        print('in B bar')
    def hello(self):
        print('B hello')

class C(B, A):
    pass
    # def hello(self):
    #     print('C hello')

if __name__ == '__main__':
    c = C()
    c.foo()
    c.bar()
    c.hello()
上一篇 下一篇

猜你喜欢

热点阅读