Python中的多重继承是否有问题?

Bug*_*tGG 2 python multiple-inheritance python-3.x

你好我在python中搜索类继承,我发现它也支持多重继承,但不知何故似乎有问题:o我找到了一个例子:

class ParentOne:
    def __init__(self):
        print "Parent One says: Hello my child!"
        self.i = 1

    def methodOne(self):
        print self.i

class ParentTwo:
    def __init__(self):
        print "Parent Two says: Hello my child"

class Child(ParentOne, ParentTwo):
    def __init__(self):
        print "Child Says: hello"
A=Child()
Run Code Online (Sandbox Code Playgroud)

产量

Child Says: hello
Run Code Online (Sandbox Code Playgroud)

因此,当子继承ParentOne和ParentTwo时,为什么不初始化这些类?我们应该在继承类Child中手动初始化它们吗?什么是正确的例子,所以我们可以看到只使用继承打印的所有消息?

事实上,它稍微复杂一些; 方法解析顺序动态变化以支持对super()的协作调用.这种方法在一些其他多继承语言中称为call-next-method,并且比单继承语言中的超级调用更强大.

当需要手动初始化时,它如何更强大?对不起,所有这些问题.提前致谢.

unu*_*tbu 7

super是为了什么:

class ParentOne():
    def __init__(self):
        super().__init__()        
        print("Parent One says: Hello my child!")
        self.i = 1

    def methodOne(self):
        print(self.i)

class ParentTwo():
    def __init__(self):
        super().__init__() 
        print("Parent Two says: Hello my child")

class Child(ParentOne, ParentTwo):
    def __init__(self):
        super().__init__()
        print("Child Says: hello")

A=Child()
Run Code Online (Sandbox Code Playgroud)

版画

Parent Two says: Hello my child
Parent One says: Hello my child!
Child Says: hello
Run Code Online (Sandbox Code Playgroud)