Python(多重继承与super())

2017-02-18  本文已影响218人  JetLu

仅作记录之用。

更多细节:How does Python's super() work with multiple inheritance?

代码一:

# coding: utf-8

class A:
    def __init__(self):
        print('A')
        self.name = 'A'

    def echo(self):
        print('name: %s' % self.name)

class B:
    def __init__(self):
        print('B')

class C(B, A):
    def __init__(self):
        super().__init__()
        print('C')

c = C()

代码二:

# coding: utf-8

class A:
    def __init__(self):
        print('A')
        self.name = 'A'

    def echo(self):
        print('name: %s' % self.name)

class B(A):
    def __init__(self):
        super().__init__()
        print('B')

class C(B, A):
    def __init__(self):
        super().__init__()
        print('C')

c = C()
上一篇下一篇

猜你喜欢

热点阅读