python - Python 3.6 (Spyder) 中的类和继承

标签 python class object inheritance attributes

我有一个 Python 初学者问题。我有一个名为“Animal”的父类(super class),并且我正在使用继承来创建一个“Dog”类。

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

    #constructor
    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 set_height(self,height):
        self.__height = height

    def get_height(self):
        return self.__height

    def set_weight(self,weight):
        self.__weight = weight

    def get_weight(self):
        return self.__weight

    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 says 
{}".format(self.__name,self.__height,self.__weight,self.__sound)

human = Animal("Rover",55,25,"woof")

print(human.toString())

#Inheritance
class Dog(Animal):
    __owner = "" #Inherit all variables from Animal class. Add an owner 
 variable. Every dog class has an owner variable, but not every
             #animal class has an owner variable.

    #overwrite the constructor 
    def __init__(self, name, height, weight, sound, owner):
        self.__owner = owner
        super(Dog,self).__init__(name, height, weight, sound) #let Animal 
superclass handle the other variables.

    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 {} kg and says {}. His owner is 
{}".format(self.__name,self.__height,self.__weight,self.__sound,self.__owner)

 myDog = Dog("Rover",55,25,"woof","Alex")

 print(myDog.toString())

我收到以下错误:

AttributeError: 'Dog' object has no attribute '_Dog__name'

Dog 子类是否无法从 Animal 父类(super class)继承 name 变量?

我正在使用 Python 3.6 和 Spyder 3.2.3

最佳答案

您无需在 Python 中执行任何此操作。无论谁教你编程,他似乎都想写 Java,而不是 Python。在 Python 中你只需要这样写:

class Animal:
    def __init__(self, name, height, weight, sound):
        self.name = name
        self.height = height
        self.weight = weight
        self.sound = sound

    def __str__(self):
        return "{} is {} cm tall and {} kg and says {}".format(self.name,self.height,self.weight,self.sound)


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

    def __str__(self):
        return "{} is {} cm tall and {} kg and says {}. His owner is  {}".format(self.name,self.height,self.weight,self.sound,self.owner)


my_dog = Dog("Rover",55,25,"woof","Alex")
print(my_dog)

关于python - Python 3.6 (Spyder) 中的类和继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49857568/

相关文章:

python - 无法在 Python 中定义基本函数

javascript - Jquery 在类选择器中添加+或减号

python - Python 中实例方法内部 self 的用法

python - XPATH:段落中的特定单词

python - 如何将 dtype(uint16) 数据转换为 16 位 png 图像?

java - Android:何时开始使用 .在 Android list 中?

javascript - React 16.3 类方法与构造函数方法

java - 从内存中的对象访问静态变量

c++ - 一个类应该管理它的实例化对象吗?

python - 模型表单在 django 中的外键上崩溃