gpt4 book ai didi

python - 类型错误 : object () takes no parameters - python

转载 作者:太空宇宙 更新时间:2023-11-04 07:16:50 25 4
gpt4 key购买 nike

我是 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 类中

关于python - 类型错误 : object () takes no parameters - python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41106533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com