我如何从多个具有不同的\uu init_uu输入的类继承?

2024-04-24 18:54:10 发布

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

我希望从两个不同的类继承它们的__init__方法中有不同的输入。我怎样才能从他们身上继承下来呢?在

下面是一个简单的例子,说明我要实现的目标:

class a:
    def __init__(self):
        print("Hello")


class b(a):
    def __init__(self):
        super().__init__()
        print("World")


class c:
    def __init__(self, text):
        print(text)


class d(a, c):
    def __init__(self):
        super().__init__('Kronos')  # <-- This breaks (and understandably so)

虽然与许多其他的多重继承问题(即How does Python's super() work with multiple inheritance?)相似,我正在寻找如何从多个具有不同输入的类继承到它们的__init__。在


Tags: 方法textselfhello目标worldinitdef