有关python中的__init_错误的一些问题

问题描述

我收到错误:TypeError: init ()缺少1个必需的位置参数:'attack'

let updatedkey = randomkey.slice(0,20) + "_" + randomkey.slice(20);

错误来自我写的 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,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,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.__init__(self,attack)。此代码有什么问题?

解决方法

您试图将Power Class继承到除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,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,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)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...