Python 子类未从父类继承的属性

Python 子类未从父类继承的属性,python,inheritance,Python,Inheritance,我正在学习用python编写代码。目前,我在障碍和类,但我有这个问题,属性不转移从父,有时它奇怪的工作。有什么问题吗 >>> class Things: pass >>> class Inanimate(Things): pass >>> class Animate(Things): pass >>> class Animals(Animate): pass >>>

我正在学习用python编写代码。目前,我在障碍和类,但我有这个问题,属性不转移从父,有时它奇怪的工作。有什么问题吗

>>> class Things:
    pass

>>> class Inanimate(Things):
    pass

>>> class Animate(Things):
    pass

>>> class Animals(Animate):
    pass

>>> class Mammals(Animals):
    pass

>>> class Giraffes(Mammals):
    pass

>>> class Animals(Animate):
    def breathe(self):
        print("breathes")


>>> class Animals(Animate):
    def move(self):
        print("moves")


>>> class Animals(Animate):
    def eat_food(self):
        print("eats food")


>>> class Animals(Animate):
    def jump(self):
        print("jumps in the air")


>>> class Mammals(Animals):
    def feeds_young_with_milk(self):
        print("feeds young with milk")


>>> class Giraffes(Mammals):
    def eat_leaves_from_trees(self):
        print("eat leaves from trees")


>>> reginald = Giraffes()
>>> reginald.move()
Traceback (most recent call last):
  File "<pyshell#44>", line 1, in <module>
    reginald.move()
AttributeError: 'Giraffes' object has no attribute 'move'
>>> reginal.breathes()
Traceback (most recent call last):
  File "<pyshell#45>", line 1, in <module>
    reginal.breathes()
NameError: name 'reginal' is not defined
>>> reginald.breathes()
Traceback (most recent call last):
  File "<pyshell#46>", line 1, in <module>
    reginald.breathes()
AttributeError: 'Giraffes' object has no attribute 'breathes'
>>> reginald.eat_food()
Traceback (most recent call last):
  File "<pyshell#47>", line 1, in <module>
    reginald.eat_food()
AttributeError: 'Giraffes' object has no attribute 'eat_food'
>>> reginald.jump()
jumps in the air
>>> 
>>类事物:
通过
>>>类无生命(物):
通过
>>>类动画(事物):
通过
>>>类动物(动画):
通过
>>>哺乳类(动物):
通过
>>>长颈鹿类(哺乳动物):
通过
>>>类动物(动画):
def呼吸(自我):
打印(“呼吸”)
>>>类动物(动画):
def移动(自我):
打印(“移动”)
>>>类动物(动画):
def eat_食物(自我):
打印(“吃食物”)
>>>类动物(动画):
def跳转(自):
打印(“空中跳跃”)
>>>哺乳类(动物):
def用牛奶喂养_young_(自身):
印刷品(“用牛奶喂养幼崽”)
>>>长颈鹿类(哺乳动物):
def吃树上的叶子(self):
打印(“吃树上的叶子”)
>>>reginald=长颈鹿()
>>>reginald.move()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
reginald.move()
AttributeError:“长颈鹿”对象没有属性“移动”
>>>区域性呼吸
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
区域性呼吸
NameError:未定义名称“reginal”
>>>雷金纳德呼吸
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
雷金纳德呼吸
AttributeError:“长颈鹿”对象没有属性“呼吸”
>>>雷金纳德:吃东西
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
雷金纳德:吃东西
AttributeError:“长颈鹿”对象没有“吃食物”属性
>>>reginald.jump()
空中跳跃
>>> 

与Ruby不同,重新定义类不会在现有定义中添加更多内容。每次编写
类动物(动画):…
,您都在定义一个全新的类,该类与具有该名称的任何以前的类都没有连接,并替换名称
动物
用于引用的类对象。新类没有旧类的方法


停止重新定义相同的类5次。第一次完全定义一个类。

为什么要定义同一个类4次?您已经定义了
动物
类4次,每次都覆盖上一个类。