Python class 继承查询

Python class Inheritance query

我在 python 多级 class 继承中遇到错误。
这是我的代码:

class Animal():
    def __init__(self):
        print("Animal created")
    def whoAmI(self):
        print("Animal")
    def eat(self):
        print('eating')

class Dog(Animal):
    print("dog created")

class Cat(Dog):
    print("car created")

m = Cat()
Cat.eat()

这是我收到的错误:

你的代码应该是这样的:

m = Cat()
m.eat()

m 是 class Cat 的一个实例,因此,您可以在其上调用 eat()。除非你说 Cat().eat().
,否则你不能在 Cat 本身上调用 eat 这与继承关系不大,因为这段代码也会给你一个错误:

Animal.eat()

此外,cat 应该直接继承自 Animal 而不是 dog。