Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python的派生类中,如何调用具有相同名称方法的基类的方法?_Python_Inheritance_Methods - Fatal编程技术网

在python的派生类中,如何调用具有相同名称方法的基类的方法?

在python的派生类中,如何调用具有相同名称方法的基类的方法?,python,inheritance,methods,Python,Inheritance,Methods,代码:- 在这里,当我用mydog对象调用eat()方法时,它会打印“Dog Eating”,有没有办法用mydog对象调用基础动物类的eat()方法,比如有这样的东西 class Animal(): def __init__(self) -> None: print("Animal Created") def eat(self): print("An

代码:-

在这里,当我用mydog对象调用eat()方法时,它会打印“Dog Eating”,有没有办法用mydog对象调用基础动物类的eat()方法,比如有这样的东西

class Animal():
        
    def __init__(self) -> None:
        print("Animal Created")
        
            
    def eat(self):
        print("Animal Eating")
            
class Dog(Animal):
    
    def __init__(self) -> None:
        # Animal.__init__(self)
        print ("Dog Created")
        
    def eat(self):
        print("Dog Eating")
    
mydog = Dog()
mydog.eat()

我不想使用super(),因为它会从子类中调用eat(),所以它会打印“吃动物”和“吃狗”这两个语句,我不想,我只想一次调用一个。

是的,您可以直接调用
动物的
eat
方法,将对象作为参数传递

mydog.Animal.eat() or mydog.eat(Animal)
虽然如果你给方法起不同的名字,可能会减少代码的混乱。

Animal.eat(mydog)
它应该显示“Dog Eating”,因为你有一只狗,它被基本方法覆盖了。
Animal.eat(mydog)