小编Flu*_*ert的帖子

Python继承返回属性错误

刚刚开始学习 Python,我是 Derek Banas 的粉丝,一直在遵循教程,但我被一些代码困住了。

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")

    def tostring(self):
        return "{} is {} cm tall and {} kilograms and say {} His owner is {}".format(self.__name,
                                                                                     self.__height,
                                                                                     self.__weight,
                                                                                     self.__sound,
                                                                                     self.__owner)

    def multiple_sounds(self, how_many=None):
        if how_many is None:
            print(self.get_sound())
        else:
            print(self.get_sound() * how_many)

spot = Dog("Spot", 53, 27, "Ruff", "Seb")

print(spot.tostring()) …
Run Code Online (Sandbox Code Playgroud)

python inheritance

5
推荐指数
1
解决办法
1477
查看次数

标签 统计

inheritance ×1

python ×1