Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python类_init__重写,TypeError:_init__()接受5个位置参数,但给出了6个_Python_Python 3.x - Fatal编程技术网

Python类_init__重写,TypeError:_init__()接受5个位置参数,但给出了6个

Python类_init__重写,TypeError:_init__()接受5个位置参数,但给出了6个,python,python-3.x,Python,Python 3.x,我学了几天python,试图为现有类创建一个子类,但找不到错误。我什么都试过了,但我觉得我只是瞎了 输出总是给出这样的错误 dog = Dog('Max', 120, 50, 'Woff', 'Tom') TypeError: __init__() takes 5 positional arguments but 6 were given 这是密码,加菲猫的动物部分工作正常 class Dog(Animal): __owner = "" def __init(self, na

我学了几天python,试图为现有类创建一个子类,但找不到错误。我什么都试过了,但我觉得我只是瞎了

输出总是给出这样的错误

dog = Dog('Max', 120, 50, 'Woff', 'Tom')
TypeError: __init__() takes 5 positional arguments but 6 were given
这是密码,加菲猫的动物部分工作正常

class Dog(Animal):
    __owner = ""

    def __init(self, name, height, weight, sound, owner):
        self.__owner = owner
        super(Dog, self).__init__(name, height, weight, sound)

dog = Dog('Max', 120, 50, 'Woff', 'Tom') #<==== HERE ERROR OCCURES
类狗(动物):
__owner=“”
定义初始值(自我、姓名、身高、体重、声音、所有者):
self.\uuu owner=所有者
超级(狗,自己)。\uuuu初始(姓名、身高、体重、声音)

dog=dog('Max',120,50,'Woff',Tom')#dog中的方法init也应该在末尾加上_u:

class Dog(Animal):
    __owner = ""

# here
def __init__(self, name, height, weight, sound, owner):
    self.__owner = owner
    super(Dog, self).__init__(name, height, weight, sound)

dog类的init方法的名称错误,因此它不会重写基类init,基类init包含5个参数

def __init(self, name, height, weight, sound, owner):
    self.__owner = owner
    super(Dog, self).__init__(name, height, weight, sound)

函数结尾处缺少
\uuu
。它应该是
def\uuuu init\uuuu(..)

发布
狗的源代码
class@GarbageCollector它埋在那里,靠近底部。OP,大多数发布的方法与您的问题无关,请考虑将代码只删除相关部分。至于缩进,您只需要复制/粘贴代码块,一次性高亮显示所有代码块,然后单击编辑器中的
{}
按钮或使用ctrl+k。在类级别定义所有者是没有意义的。更不用说给它一个双下划线前缀了。顺便说一句,
toSting
应该是
\uuuu str\uuu
。这样,您就可以将类实例传递给
str()
,或者打印它们。此外,我们通常不会在Python中为简单属性实现getter和setter,而是直接访问属性。如果您确实需要实际的getter和setter方法,那么最好将它们实现为属性,这样调用方仍然可以像访问简单属性一样访问它们。谢谢!谁会赢?一堆程序或两个下划线?它还需要在Dog类中使用一次,而不是在全局级别。现在,此函数给出一个错误:
def toString(self):返回“{}是{}厘米高和{}公斤,并说{}和他的主人是{}”。格式(self.\uu名称,self.\uuu高度,self.\uuu重量,self.\uu声音,self.\uu所有者)
打印出来:
返回“{}是{}厘米高,{}公斤,说{}他的主人是{}”。格式(self.\uuu name,self.\uu height,self.\uu weight,self.\uu sound,self.\uu owner)属性错误:“Dog”对象没有属性“\uu Dog\uu name”
@marcinmacki
格式(self.name,self.height,self.weight,self.sound,self.owner)