简体   繁体   中英

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. How can I force it to call the base class method1? Is it best for the inner class to have an init and from there call the parent 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.

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 .

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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