我正在用python学习OOP。在下面的代码中创建,以在多个继承中复制菱形问题。我在jupyter笔记本中运行以下代码,并且在同一时间生成输出。

class parent:
    def __init__(self):
        self.a=2
        self.b=4
    def form1(self):
        print("calling parent from1")
        print('p',self.a+self.b)

class child1(parent):
    def __init__(self):
        self.a=50
        self.b=4
    def form1(self):
        print('bye',self.a-self.b)
    def callchildform1(self):
        print("calling parent from child1")
        super().form1()

class child2(parent):
    def __init__(self):
        self.a=3
        self.b=4
    def form1(self):
        print('hi',self.a*self.b)
    def callchildform1(self):
        print("calling parent from child2")
        super().form1()

class grandchild(child1,child2):
    def __init__(self):
        self.a=10
        self.b=4
    def callingparent(self):
        super().form1()

g=grandchild()
g.form1()
g.callchildform1()
g.callingparent()


输出如下

bye 6
calling parent from child1
hi 40
bye 6


我可以同时理解“ bye 6”输出,但它如何打印“ hi 40”。我是新来的,所以任何人都可以解释这里发生的事情。

最佳答案

您可能会找到信息类的__mro__属性。在此,MRO代表“方法解析顺序”。

考虑对代码的此修改:

class Parent:
    def __init__(self):
        self.a = 2
        self.b = 4

    def print_name(self):
        print("parent")

    def form1(self):
        print("calling parent form1")
        print('p', self.a + self.b)


class Child1(Parent):
    def __init__(self):
        self.a = 50
        self.b = 4

    def print_name(self):
        print("child1")

    def print_super_name(self):
        super().print_name()

    def form1(self):
        print('bye', self.a - self.b)

    def callchildform1(self):
        print("calling parent from child1")
        super().form1()


class Child2(Parent):
    def __init__(self):
        self.a = 3
        self.b = 4

    def print_name(self):
        print("child2")

    def form1(self):
        print('hi', self.a * self.b)

    def callchildform1(self):
        print("calling parent from child2")
        super().form1()


class Grandchild(Child1, Child2):
    def __init__(self):
        self.a = 10
        self.b = 4

    def print_name(self):
        print("grandchild")

    def print_super_name(self):
        super().print_name()

    def print_super_super_name(self):
        super().print_super_name()

    def callingparent(self):
        super().form1()


g = Grandchild()
print("When I print the name of my class it is:")
g.print_name()
print("When I print my superclass name, it is:")
g.print_super_name()
print("When I print the name of the superclass of my superclass, it is:")
g.print_super_super_name()
print("When you call methods on me, they will be executed from my class and my parent classes in the following order:")
print(Grandchild.__mro__)
g.form1()
g.callchildform1()
g.callingparent()


输出为:

When I print the name of my class it is:
grandchild
When I print my superclass name, it is:
child1
When I print the name of the superclass of my superclass, it is:
child2
When you call methods on me, they will be executed from my class and my parent classes in the following order:
(<class '__main__.Grandchild'>, <class '__main__.Child1'>, <class '__main__.Child2'>, <class '__main__.Parent'>, <class 'object'>)
bye 6
calling parent from child1
hi 40
bye 6


当您运行g.callchildform1()时,Python将在callchildform1中查找Grandchild的定义。它不在那里,所以下一个看起来是Child1。您可以从示例和方法解析顺序中看到,当Grandchild的实例调用Child1中定义的方法并调用super()时,对被调用方法的搜索将从Child2开始。

10-06 08:15