简体   繁体   English

从基类调用基类方法

[英]Calling a base class method from a base class

When I call method1() from the outer class, it ends up calling the derived class method1() instead. 当我从外部类调用method1()时,它将最终调用派生类method1()。 How can I force it to call the base class method1? 如何强制它调用基类method1? Is it best for the inner class to have an init and from there call the parent init ? 内部类最好有一个init并从那里调用父init吗?

class OuterClassA
    __init__
       method1()

    def method1(self):
        ....

class InnerClassB(OuterClassA)

    def method1(self):
     ....

Python's double underscores name mangling is designed to help with this issue. Python的双下划线名称改写旨在帮助解决此问题。

For the details and a worked-out example see: http://docs.python.org/tutorial/classes.html#private-variables and at http://docs.python.org/reference/expressions.html#atom-identifiers . 有关详细信息和可行的示例,请参见: http : //docs.python.org/tutorial/classes.html#private-variableshttp://docs.python.org/reference/expressions.html#atom-标识符

class OuterClassA:
    def __init__(self):
        self.__method1()      # call this class's private copy

    def method1(self):
        ...
    __method1 = method1       # make a private (class local) copy


class InnerClassB(OuterClassA)
    def method1(self):
        ...

调用基类method1()

OuterClassA.method1(someClassBObject)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM