Python super().__init__() 具有多重继承

问题描述 投票:0回答:2
class School:
    def __init__(self, school_name):
        self._school = school_name

class Exam:
    def __init__(self, exam_name):
        self._exam_name = exam_name
    def credit(self):
        return 3

class Test(School, Exam):
    def __init__(self, school_name, exam_name):
        self._date = "Oct 7"
        super().__init__(school_name, exam_name)

test = Test('Success', 'Math')
print(test._school) 
print(test._exam_name) 

我只是想知道为什么 super().init() 不能在这里工作。如果我坚持使用 super(),正确的方法是什么才能成功传递 school_name 和 exam_name。

python-3.x class multiple-inheritance
2个回答
1
投票

Super函数仅按照MRO顺序调用父类。据此,您首选的班级将是 School,并且将调用 School 的 init。调用

super().__init__(self,school_name)
时,您只需提供 school_name 作为参数。但如果你想调用特定的
__init__()
,那么最好使用
<parent_class>.<method>(<parameters>)
。如果您想调用这两个 init 函数,请尝试这样做:

class School:
    def __init__(self, school_name):
        self._school = school_name

class Exam:
    def __init__(self, exam_name):
        self._exam_name = exam_name
    def credit(self):
        return 3

class Test(School, Exam):
    def __init__(self, school_name, exam_name):
        self._date = "Oct 7"
        School.__init__(self,school_name)
        Exam.__init__(self,exam_name)

test = Test('Success', 'Math')
print(test._school) 
print(test._exam_name)

尝试这样做


0
投票

如果你想继承2个类,也可以这样做

class B:
    def __init__(self, param1='hello from B'):
        print('start B __init__')`enter code here`
        self.param1=param1
class C:
    def __init__(self, param2='hello from C'):
        print('start C __init__')
        super().__init__()
        print('end C __init__')
        self.param2 = param2

class D(C,B):
    def __init__(self, param='hello from D'):
        super().__init__()
        print('start D __init__')
        print(self.param1,self.param2)
d = D()

C_class中的super().init调用了B_Class的构造函数

可以插入任意数量的函数,只要继承的类调用下一个函数即可

class B:
    def __init__(self, param1='hello from B'):
        print('start B __init__')
        self.param1=param1
        
class C:
    def __init__(self, param2='hello from C'):
        print('start C __init__')
        super().__init__()
        print('end C __init__')
        self.param2 = param2
        
class E:
    def __init__(self, param3='hello from E'):
        print('start E __init__')
        super().__init__()
        print('end E __init__')
        self.param3=param3

class D(C,E,B):
    def __init__(self, param='hello from D'):
        super().__init__()
        print('start D __init__')
        print(self.param1,self.param2)
d = D()

如果你不想关注哪个类先出现,你可以为所有类提供 super()._init()

class B:
    def __init__(self, param1='hello'):
        print('start B __init__')
        super().__init__()
        self.param1=param1

那你可以吗

class D(E,C,B):
    def __init__(self, param='hello from D'):
        super().__init__()
class D(B,C,E):
    def __init__(self, param='hello from D'):
        super().__init__()
class D(C,B,E):
    def __init__(self, param='hello from D'):
        super().__init__()
© www.soinside.com 2019 - 2024. All rights reserved.