TypeError:对象()不接受任何参数-python

TypeError:对象()不接受任何参数-python,python,Python,我是python新手,试图用类和对象编写程序。以下是我的代码: class Animal: __name = "" __height = 0 __weight = 0 __sound = 0 # define a constructor and pass arguments def __init__(self, name, height, weight, sound): self.__name = name self.__height = hei

我是python新手,试图用类和对象编写程序。以下是我的代码:

class Animal:
    __name = ""
    __height = 0
    __weight = 0
    __sound = 0

# define a constructor and pass arguments
def __init__(self, name, height, weight, sound):
    self.__name = name
    self.__height = height
    self.__weight = weight
    self.__sound = sound


# set and get functions for name
def set_name(self, name):
    self.__name = name

def get_name(self):
    return self.__name


# set and get functions for height
def set_height(self, height):
    self.__height = height

def get_height(self):
    return str(self.__height)


# set and get functions for weight
def set_weight(self, weight):
    self.__weight = weight


def get_weight(self):
    return str(self.__weight)


# set and get functions for sound
def set_sound(self, sound):
    self.__sound = sound

def get_sound(self):
    return self.__sound


def get_type(self):
    print("Animal")


def toString(self):
    return "{} is {} cm tall and {} kg and say {}".format(self.__name, self.__height, self.__weight, self.__sound)


# create an object call cat of type Animal
cat = Animal('Whiskers', 33, 10, 'Meow')
print(cat.toString())

当我运行程序时,它会给我一个错误,对象不带参数。但是我已经在类的构造函数中描述了参数。请帮助。

确保所有动物功能都正确缩进。它们需要在类Animal中

确保所有针对Animal的函数都正确缩进。它们需要位于类Animal的内部

正如其他人所提到的,
\uuuuu init\uuuu
方法需要缩进类Animal的内部,以便将其视为构造函数


但是,这样做之后要小心。现在声明
\uu name
\uu height
\uu weight
\uu sound
的方式是静态类变量。但是,在
\uuuuu init\uuuuuu
中(通过在变量前面加前缀
self.
),您将用变量名重新声明实例变量。

正如其他人所提到的,
\uuuuu init\uuuuu
方法需要在类Animal中缩进,以便将其视为构造函数


但是,这样做之后要小心。现在声明
\uu name
\uu height
\uu weight
\uu sound
的方式是静态类变量。但是,在
\uuuu init\uuuuu
中(在变量前面加上前缀
self.
),您将用变量名重新声明实例变量。

您的代码是否按此处发布的方式缩进?如果是这样,那就是你的问题。。。。因为如果是这样,那么
\uuuuu init\uuuuu()
不是一个方法,而是一个简单的函数,
Animal
只从对象继承
\uuuuuuu()
,因此会出现错误消息。请尝试从较小的位置开始。对于您的错误,没有理由只使用构造函数。除此之外,Python不是Java。在Java中重写
toString
与在Pythoncan中实现
\uuu str\uuu
是一样的,请详细解释一下。我是python新手,试图从错误中吸取教训。缩进代码是什么意思??我能做些什么来改进它呢?空白是非常重要的。通读教程。你有没有按照贴在这里的方式缩进你的代码?如果是这样,那就是你的问题。。。。因为如果是这样,那么
\uuuuu init\uuuuu()
不是一个方法,而是一个简单的函数,
Animal
只从对象继承
\uuuuuuu()
,因此会出现错误消息。请尝试从较小的位置开始。对于您的错误,没有理由只使用构造函数。除此之外,Python不是Java。在Java中重写
toString
与在Pythoncan中实现
\uuu str\uuu
是一样的,请详细解释一下。我是python新手,试图从错误中吸取教训。缩进代码是什么意思??我能做些什么来改进它呢?空白是非常重要的。通读教程。