[英]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.