首先,这是我的代码:
class Enemy():
def __init__(self, name, hp, damage):
self.name = name
self.hp = hp
self.damage = damage
def is_alive(self):
"""Checks if alive"""
return self.hp > 0
class WildBoar(Enemy):
def __init__(self):
super(WildBoar, name="Wild Boar", hp=10, damage=2).__init__()
class Marauder(Enemy):
def __init__(self):
super(Marauder, name="Marauder", hp=20, damage=5).__init__()
class Kidnappers(Enemy):
def __init__(self):
super(Kidnappers, name="The Kidnappers", hp=30, damage=7).__init__()
当我编译这个时,我收到此错误:
super(WildBoar, name="Wild Boar", hp=10, damage=2).__init__()
TypeError: super does not take keyword arguments
我尝试四处寻找任何帮助,但一无所获。我在其他班级的超级中也有一些 Kwargs,但这些是引发任何类型问题的人(截至目前)。那么什么可能导致这种情况呢?我还看到有人说放一个super
在基类中将修复它,但它不起作用(我传递了与基类中相同的参数__init__
).
针对家长的论据__init__
方法应该传递给__init__
method:
super(Kidnappers, self).__init__(name="The Kidnappers", hp=30, damage=7)
# or
super(Kidnappers, self).__init__("The Kidnappers", 30, 7)
所有你传递给super()
是子类(Kidnappers
在本例中)和对当前实例的引用(self
).
但请注意,如果您使用的是 Python 3.x,您需要做的就是:
super().__init__("The Kidnappers", 30, 7)
Python 将解决剩下的问题。
以下是文档中对此进行解释的一些链接:
- Python 2.xsuper() https://docs.python.org/2/library/functions.html#super
- Python 3.xsuper() https://docs.python.org/3/library/functions.html#super
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)