Python 中的多重继承 - TypeError

Multiple Inheritance in Python - TypeError

真的为此苦苦挣扎。过去几天一直在窃听,我无法理解它,也找不到对我来说完全有意义的答案。

我只是真正了解 OOP,而且我一直在研究多重继承。我遇到的问题是,当我实例化继承自多个 class 的 class 时,出现以下错误:

TypeError: Wizard.__init__() missing 1 required positional argument: 'power'

其他 classes 工作得很好,只是当我实例化 'Hybrid' class 时,我得到了错误。

代码如下:

class User:
    def __init__(self, name):
        self.name = name

    def greet(self):
        return f"Welcome, {self.name.title()}."


class Archer(User):
    def __init__(self, name, arrows):
        super().__init__(name)
        self.arrows = arrows

    def shoot_arrow(self):
        self.arrows -= 1
        return "You shot an arrow!"

    def arrows_left(self):
        return f"You have {self.arrows} arrows remaining."


class Wizard(User):
    def __init__(self, name, power):
        super().__init__(name)
        self.power = power

    def cast_spell(self):
        return f"You cast a spell with a power of {self.power}"


class Hybrid(Archer, Wizard):
    def __init__(self, name, arrows, power):
        Archer.__init__(self, name, arrows)
        Wizard.__init__(self, name, power)

    def powerful(self):
        return "Hybrids are super powerful!"


merlin = Wizard('merlin', 1000)
robin = Archer('robin', 150)
hawk = Hybrid('hawk', 200, 650)


print(merlin.greet())
print(merlin.cast_spell())

print(robin.arrows_left())
print(robin.shoot_arrow())
print(robin.arrows_left())

任何帮助都将是惊人的,对于这个领域的新手可以理解的任何解释将不胜感激。

您现在拥有的 classes 不适合用于多重继承。当使用多重继承时,我更喜欢让构造函数同意相同的契约(使用相同的参数)或者根本没有参数。这里 powersarrows 不同,这使得调用每个构造函数变得尴尬。

IMO 设计此 class 的更好方法是混入。 mixin 将没有构造函数,并依赖于扩展它们的 classes 中存在的特定值。

示例混合:

class UserMixin:
    name: str

    def greet(self):
        return f"Welcome, {self.name.title()}."


class ArcherMixin(UserMixin):
    arrows: int

    def shoot_arrow(self):
        self.arrows -= 1
        return "You shot an arrow!"

    def arrows_left(self):
        return f"You have {self.arrows} arrows remaining."


class WizardMixin(UserMixin):
    power: int

    def cast_spell(self):
        return f"You cast a spell with a power of {self.power}"

示例实现:

class User(UserMixin):
    def __init__(self, name):
        self.name = name


class Archer(ArcherMixin):
    def __init__(self, name, arrows):
        self.name = name
        self.arrows = arrows


class Wizard(WizardMixin):
    def __init__(self, name, power):
        self.name = name
        self.power = power


class Hybrid(ArcherMixin, WizardMixin):
    def __init__(self, name, arrows, power):
        self.name = name
        self.arrows = arrows
        self.power = power

    def powerful(self):
        return "Hybrids are super powerful!"

用法示例:

merlin = Wizard('merlin', 1000)
robin = Archer('robin', 150)
hawk = Hybrid('hawk', 200, 650)

print(merlin.greet())
print(merlin.cast_spell())

print(robin.greet())
print(robin.arrows_left())
print(robin.shoot_arrow())
print(robin.arrows_left())

print(hawk.greet())
print(hawk.cast_spell())
print(hawk.arrows_left())
print(hawk.shoot_arrow())
print(hawk.arrows_left())
Welcome, Merlin.
You cast a spell with a power of 1000
Welcome, Robin.
You have 150 arrows remaining.
You shot an arrow!
You have 149 arrows remaining.
Welcome, Hawk.
You cast a spell with a power of 650
You have 200 arrows remaining.
You shot an arrow!
You have 199 arrows remaining.