简体   繁体   English

在继承的情况下,是否可以通过在python中使用子类对象引用来调用第二个父类方法? 如果是,那怎么办?

[英]Is it possible to call 2nd parent class method by using child class object reference inside python in case of inheritance? If yes then how?

I'm recently started exploring python technology. 我最近开始探索python技术。 I found a problem when I doing my exercise. 我做运动时发现一个问题。

Suppose we have two class with different name but with same name method is exist in both class with no arguments. 假设我们有两个具有不同名称的类,但两个类中都存在具有相同名称的方法,且没有参数。 Example: 例:

class Parent_1: # define parent class
    def myMethod(self):
        print 'Calling parent method_1'

another is 另一个是

class Parent_2: # define parent class
    def myMethod(self):
        print 'Calling parent method_2'

I have another class (child class) which inherit these both classes. 我还有另一个类(子类),它们继承了这两个类。

class Child(Parent_1, Parent_2): # define child class
    print "abc"
    #Parent_1().myMethod();
    #Parent_2().myMethod();

see here, if i try to call 2nd class method then i can call with the 2nd parent class reference inside child class. 看到这里,如果我尝试调用第二类方法,那么我可以在子类中使用第二个父类引用进行调用。 But when i'm trying to call from outside by using child class object reference. 但是,当我尝试通过使用子类对象引用从外部进行调用时。

c = Child()
c.myMethod()

Output is: 输出为:

abc
Calling parent method_1

here, you can watch it will call 1st parent class method by default using child class reference. 在这里,您可以看到它将默认使用子类引用调用第一个父类方法。

What if i wanna to call same method of another class using child class reference explicitly without changing inherited base classes order. 如果我想使用子类引用显式调用另一个类的相同方法而不更改继承的基类顺序,该怎么办。

Is it possible or not ? 有可能吗? if yes then how ? 如果是,那怎么办?

Thanks in advanced & please give me your valuable answers regarding this question and suggest me if I forgot to mention anything here. 在此先感谢您,并给我您有关此问题的宝贵答案,如果我忘记在这里提及任何内容,请提出建议。 Best suggestion or answer will be appreciated. 最好的建议或答案将不胜感激。

您可以调用unbound函数并显式传递self参数:

Parent_2.myMethod(c)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在python中用子类对象调用父类的重写方法 - Call overridden method of parent class with child class object in python Python继承:在父类中创建子对象 - Python inheritance: create child object in parent class 如何在Python的子类中调用和覆盖父类方法 - How to call and override a Parent class method from Child class in Python 如何在python中使用子类的方法调用第二个父类的方法? - How to call method of second parent class using method of child class in python? 如何使用 python 中的子 class 的方法调用第三个或更高的父 class 的方法? - How to call method of third or higher parent class using method of child class in python? I want to call parent class method which is overridden in child class through child class object in Python - I want to call parent class method which is overridden in child class through child class object in Python 如何使用父类对象调用子类方法 - How to call child class methods using parent class object Python继承:从子类创建父类对象 - Python inheritance: Create parent class object from child class 如何在使用来自父对象的数据时更改父/子类结构的不同方法调用的代码 - How to change code for different method call of a parent/child class structure while using data from parent object Python父/子类方法调用 - Python Parent/Child class method call
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM