多重继承 Python __init__ 无效
Multiple Inheritance Python __init__ not working
我有这 2 个 classes:
from avatar import Avatar
class Caster(Avatar):
def __init__(self, name, life, strength, protection, mana):
super().__init__(name, life, strength, protection)
self._mana = mana
和:
from avatar import Avatar
class Melee(Avatar):
def __init__(self, name, life, strength, protection, shield):
super().__init__(name, life, strength, protection)
self._shield = shield
他们都继承自:
class Avatar:
def __init__(self, name, life, strength, protection):
self._name = name
self._life = life
self._strength = strength
self._protection = protection
现在我想创建一个新的 class 继承自 Caster 和 Melee
from melee import Melee
from caster import Caster
class Chaman(Melee, Caster):
def __init__(self, name, life, strength, protection, shield, mana):
Melee().__init__(self, name, life, strength, protection, shield)
Caster().__init__(self, mana)
这个新的 class 结合了近战和施法者的元素。当我执行它并尝试创建新的 Chaman 对象时,它给了我这个错误:
TypeError: __init__() missing 5 required positional arguments: 'name', 'life', 'strength', 'protection', and 'shield'
设计您的 classes 并正确使用 super
,如 Python's super()
considered super!.
中的进一步详细讨论
class Avatar:
def __init__(self, *, name, life, strength, protection, **kwargs):
super().__init__(**kwargs)
self._name = name
self._life = life
self._strength = strength
self._protection = protection
class Caster(Avatar):
def __init__(self, *, mana, **kwargs):
super().__init__(**kwargs)
self._mana = mana
class Melee(Avatar):
def __init__(self, *, shield, **kwargs):
super().__init__(**kwargs)
self._shield = shield
class Chaman(Melee, Caster):
pass
# Note that the order of the *keyword* arguments does not matter.
c = Chaman(name="bob", life=3, strength=10, protection=5, mana=9, shield=0)
每个 class 只处理它引入的参数,通过 super
.
将任何其他参数传递到堆栈中
由于Chaman.__init__
没有定义,Melee.__init__
会先被调用;它会调用 Caster.__init__
(not Avatar.__init__
),后者会调用 Avatar.__init__
,后者会调用 object.__init__
,后者什么都不做。
我有这 2 个 classes:
from avatar import Avatar
class Caster(Avatar):
def __init__(self, name, life, strength, protection, mana):
super().__init__(name, life, strength, protection)
self._mana = mana
和:
from avatar import Avatar
class Melee(Avatar):
def __init__(self, name, life, strength, protection, shield):
super().__init__(name, life, strength, protection)
self._shield = shield
他们都继承自:
class Avatar:
def __init__(self, name, life, strength, protection):
self._name = name
self._life = life
self._strength = strength
self._protection = protection
现在我想创建一个新的 class 继承自 Caster 和 Melee
from melee import Melee
from caster import Caster
class Chaman(Melee, Caster):
def __init__(self, name, life, strength, protection, shield, mana):
Melee().__init__(self, name, life, strength, protection, shield)
Caster().__init__(self, mana)
这个新的 class 结合了近战和施法者的元素。当我执行它并尝试创建新的 Chaman 对象时,它给了我这个错误:
TypeError: __init__() missing 5 required positional arguments: 'name', 'life', 'strength', 'protection', and 'shield'
设计您的 classes 并正确使用 super
,如 Python's super()
considered super!.
class Avatar:
def __init__(self, *, name, life, strength, protection, **kwargs):
super().__init__(**kwargs)
self._name = name
self._life = life
self._strength = strength
self._protection = protection
class Caster(Avatar):
def __init__(self, *, mana, **kwargs):
super().__init__(**kwargs)
self._mana = mana
class Melee(Avatar):
def __init__(self, *, shield, **kwargs):
super().__init__(**kwargs)
self._shield = shield
class Chaman(Melee, Caster):
pass
# Note that the order of the *keyword* arguments does not matter.
c = Chaman(name="bob", life=3, strength=10, protection=5, mana=9, shield=0)
每个 class 只处理它引入的参数,通过 super
.
由于Chaman.__init__
没有定义,Melee.__init__
会先被调用;它会调用 Caster.__init__
(not Avatar.__init__
),后者会调用 Avatar.__init__
,后者会调用 object.__init__
,后者什么都不做。