在Python中,在尝试菱形问题时,OOPs发生了什么

2024-09-30 01:27:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python学习OOPs。创建下面的代码来复制多重继承中的菱形问题。我在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

我可以理解“再见6”输出的时间,但它是如何打印“高40”。我是新来的,所以任何人都可以解释这里发生了什么。你知道吗


Tags: 代码fromselfinitdefclassparentbye
1条回答
网友
1楼 · 发布于 2024-09-30 01:27:32

您可以找到类的__mro__属性。这里,MRO代表MethodResolutionOorder。你知道吗

考虑一下对代码的这种修改:

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会在Grandchild中查找callchildform1的定义。它不在那里,所以下一个看起来的地方是Child1。从示例和方法解析顺序可以看出,当Grandchild的实例调用Child1中定义的方法(该方法调用super())时,对被调用方法的搜索将从Child2开始。你知道吗

相关问题 更多 >

    热门问题