关于python中_uinit_uu错误的一些问题
我得到错误:TypeError:init()缺少1个必需的位置参数:“攻击”关于python中_uinit_uu错误的一些问题,python,python-3.x,init,Python,Python 3.x,Init,我得到错误:TypeError:init()缺少1个必需的位置参数:“攻击” class Unit: def __init__(self, name): self.name = name class Power(Unit): def __init__(self, name, attack): Unit.__init__(self, name) self.attack
class Unit:
def __init__(self, name):
self.name = name
class Power(Unit):
def __init__(self, name, attack):
Unit.__init__(self, name)
self.attack = attack
supatt = attack * 2
print("{0} has a power of {1} and it can develop {1}
of its superpowers".format(self.name, attack, supatt))
class Ground(Power):
def __init__(self, name, attack, velocity, friction):
Power.__init__(self, attack)
self.velocity = velocity
self.friction = friction
totalv = velocity - fiction
print("{0} : Groud Attack. \nTotal Speed : {1}.\n
Power : {2}.".format(self.name, totalv, attack))
class Sky(Power):
def __init__(self, name, attack, skyspeed, airres):
Power.__init__(self, attack)
self.skyspeed = skyspeed
self.airres = airres
totalss = skyspeed - airres
print("{0} : Sky Attack. \nTotal Speed : {1}.\n Power
: {2}.".format(self.name, totalss, attack))
valkyrie = Sky("Valkyrie", 200, 300, 150)
print(valkyrie)
错误出现在Sky(Power)
类中,我在其中写道:
Power.\uuuuu init\uuuuuu(自我攻击)
我想我已经在这里写了
attack
。此代码有什么问题?您试图将Power类继承到除Unit类之外的所有其他类
您希望使用super()函数继承类
class Unit:
def __init__(self, name):
self.name = name
class Power(Unit):
def __init__(self, name, attack):
Unit.__init__(self, name)
self.attack = attack
supatt = attack * 2
print("{0} has a power of {1} and it can develop {1} of its
superpowers".format(self.name, attack, supatt))
class Sky(Power):
def __init__(self, name, attack, skyspeed, airres):
super().__init__(self, attack)
self.skyspeed = skyspeed
self.airres = airres
totalss = skyspeed - airres
print("{0} : Sky Attack. \nTotal Speed : {1}.\n Power:
{2}.".format(self.name, totalss, attack))
class Ground(Power):
def __init__(self, name, attack, velocity, friction):
super().__init__(self, attack)
self.velocity = velocity
self.friction = friction
totalv = velocity - friction
print("{0} : Groud Attack. \nTotal Speed : {1}.\nPower :
{2}.".format(self.name, totalv, attack))
valkyrie = Sky("Valkyrie", 200, 300, 150)
print(valkyrie)
您的Power类还需要2个参数,而您只提供了1个。因为两者都是位置性的,所以它仍然期望传递给“攻击”的东西看起来就像你忘了传递层次结构上的
name
。Power.\uuuu init\uuu(self,name,attack)