Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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中工作?_Python_Python 2.7_Class - Fatal编程技术网

如何让类继承在Python中工作?

如何让类继承在Python中工作?,python,python-2.7,class,Python,Python 2.7,Class,我应该创建一个名为DogAnimal的类,它继承自类Animal。但是,在运行此代码后,出现了我不理解的错误: 问题解决了,我现在看不到更多的问题 class Animal: __name = "" __height = 0 __weight = 0 __sound = 0 def __init__(self, name, height, weight, sound): self.__name = name self.__height = height self.__w

我应该创建一个名为DogAnimal的类,它继承自类Animal。但是,在运行此代码后,出现了我不理解的错误:

问题解决了,我现在看不到更多的问题

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

def __init__(self, name, height, weight, sound):
    self.__name = name
    self.__height = height
    self.__weight = weight
    self.__sound = sound

#def set_name(self, name):
    #self.__name = name

def get_name(self):
    return self.__name

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

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

def get_sound(self):
    return self.__sound

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

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


cat = Animal('pussy', 33, 10, 'meow')
print(cat.toString())
print(cat.get_type())
print(cat.get_sound())

class Dog(Animal):
__owner = ""

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

def set_owner(self, owner):
    self.__owner = owner

def get_owner(self):
    return self.__owner

def get_type(self):
    print("Dog") # Dog object

def toString(self):
    return "{} is {} cm tall and {} kilograms and says{} its owner is {}".format(self.__name,
                                                                                 self.__height,
                                                                                 self.__weight,
                                                                                 self.__sound,
                                                                                 self.__owner)

spot = Dog("kaili", 22, 33, "woof", "Jiahui") 
print(spot.toString())  
更改此行:

class Animal:
为此:

class Animal(object):

这很好地解释了这个问题。

在我开始添加metaclass=type之后,问题就解决了。我不太确定,但元类似乎允许python2.7访问python3的库

请将错误堆栈跟踪作为代码段提供,而不是链接到图像。您好,您的意思是回溯吗?我在邮件上附上了一张照片。对不起,我是个新手。@Hansherry:对于像代码和控制台IO这样的文本人工制品,读者不喜欢图像。图像与剪贴簿、搜索引擎和屏幕阅读器不兼容。你会删除该图像并用文本替换它吗?对日志也使用代码格式。遗憾的是,我对上面的内容投了反对票。如果你能回答这个问题,我就不投票了。谢谢我已经删除了图片,我找到了答案。嗨,我看了错误:超级狗,赛尔夫。名字,身高,体重,声音类型错误:超级参数1必须是类型,不是classobj我很困惑超级狗,赛尔夫不工作你读了吗?你修改了你的代码吗?嗨,我把父类改成了类Animalobject,但似乎不起作用。错误消息是什么?缩进正确吗?我认为这不是缩进问题。它说:return{}是{}厘米高和{}公斤,并说{}它的所有者是{}.formatself.\uu name,AttributeError:'Dog'对象没有属性'\u Dog\uu name'