美文网首页
Python(多重继承与super())

Python(多重继承与super())

作者: JetLu | 来源:发表于2017-02-18 23:21 被阅读218次

仅作记录之用。

更多细节: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()

相关文章

网友评论

      本文标题:Python(多重继承与super())

      本文链接:https://www.haomeiwen.com/subject/sdmdwttx.html