Python 我的班级认为;“自我”;是需要赋值的参数

Python 我的班级认为;“自我”;是需要赋值的参数,python,inheritance,Python,Inheritance,我不知道为什么会这样。似乎认为“自我”需要一个论点,这毫无意义 这是我的密码: class Animal: def __init__(self): self.quality = 1 class Bear(Animal): def __init__(self): Animal.__init__(self) def getImage(self): return "bear.ppm" class Fish(Animal

我不知道为什么会这样。似乎认为“自我”需要一个论点,这毫无意义

这是我的密码:

class Animal:

    def __init__(self):
        self.quality = 1


class Bear(Animal):

    def __init__(self):
        Animal.__init__(self)

    def getImage(self):
        return "bear.ppm"

class Fish(Animal):

    def __init__(self):
        Animal.__init__(self)

    def getImage(self):
        return "fish.ppm"
我得到的错误是:

Traceback (most recent call last):

  File "<pyshell#1>", line 1, in <module>
    Bear.getImage()

TypeError: getImage() takes exactly 1 argument (0 given)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
Bear.getImage()
TypeError:getImage()正好接受1个参数(给定0)

在调用
getImage()之前,必须实例化
Bear

getImage
是一个实例方法,因此它只被设计为在
Bear
类的特定实例上调用。该实例的状态是作为
self
变量传递给
getImage
的。调用
b.getImage()
相当于:

b = Bear()
Bear.getImage(b)
因此,如果没有
Bear
的实例,就没有任何东西可以用于
self
参数,这就是为什么在调用
Bear.getImage()
时会看到异常。有关更多信息,请参阅

如果您希望能够在类
Bear
上而不是在特定实例上调用
getImage
,则需要使用
@staticmethod
装饰器将其设置为静态方法:

class Bear(Animal):

    def __init__(self):
        Animal.__init__(self)

    @staticmethod
    def getImage():
        return "bear.ppm"
然后可以调用
Bear。getImage()是一个实例方法,因此只能通过Bear类的实例化来调用它。因此,以下是如何做到这一点:

Bear().getImage()

Bear().getImage()
be = Bear()
be.getImage()