简体   繁体   English

如何在python中使用子类的方法调用第二个父类的方法?

[英]How to call method of second parent class using method of child class in python?

Below is my code: 下面是我的代码:

class Parent1(object):
    def __init__(self):
        print "!!! ___initialization Parent1___ !!!"

    def method(self):
        print "*** method of Parent1 is called ***"


class Parent2(object):
    def __init__(self):
        print "!!! ___initialization Parent2___ !!!"

    def method(self):
        print "*** method of Parent2 is called ***"

class Child(Parent1,Parent2):
    def __init__(self):
        print "!!! ___initialization Child___ !!!"

    def method(self):
        super(Child,self).method()
        print "*** method of Child is called ***"


Ch = Child()
Ch.method()

I want to call method() of Parent2 class using object of child class. 我想使用子类的对象来调用Parent2类的method() Conditions are only child class object should be created and no change in child class declaration ( class Child(Parent1,Parent2): should not changed.) 条件是仅应创建子类对象,并且子类声明中不得更改( class Child(Parent1,Parent2):不应更改。)

Parent2.method(self)

That's all you need - the instance.method() is just syntactic sugar for ClassName.method(instance) , so all you need to do is call it without the syntactic sugar and it'll do fine. 这就是您所需要的instance.method()只是ClassName.method(instance)语法糖,因此您所需要做的就是在不使用语法糖的情况下调用它,它会做的很好。

I changed the Child class to this: 我将Child类更改为:

class Child(Parent1,Parent2):
    def __init__(self):
        print "!!! ___initialization Child___ !!!"

    def method(self):
        super(Child,self).method()
        print "*** method of Child is called ***"
        Parent2.method(self)

And: 和:

# Out:
$ python c.py
!!! ___initialization Child___ !!!
*** method of Parent1 is called ***
*** method of Child is called ***
*** method of Parent2 is called ***

You get the expected output perfectly fine. 您将获得预期的输出,效果非常好。

暂无
暂无

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

相关问题 如何使用 python 中的子 class 的方法调用第三个或更高的父 class 的方法? - How to call method of third or higher parent class using method of child class in python? 如何在Python的子类中调用和覆盖父类方法 - How to call and override a Parent class method from Child class in Python Python父/子类方法调用 - Python Parent/Child class method call Python 如何从父元类调用子 class 方法 - Python How to call child class method from Parent metaclass 在Python 2中从子类调用父类方法 - Call a parent class method from a child class in Python 2 在python中用子类对象调用父类的重写方法 - Call overridden method of parent class with child class object in python 在继承的情况下,是否可以通过在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? 在Python2中,如何强制子类方法调用父方法而不明确要求最终用户包含它? - In Python2, how to force a child class method to call a parent method without explicitly requiring the end user to include it? 如何从 Python 中的子类调用父类的方法? - How do I call a parent class's method from a child class in Python? 我们可以在python 2.7中从子类的类方法中调用父类的实例方法吗? - Can we call instance method of parent class from class method of child class in python 2.7?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM