简体   繁体   English

从基类调用派生类中的Overriden方法

[英]Calling Overriden Methods in Derived Class from Base Class

I was reading the Python docs about classes and came across this paragraph which I'm not sure about: 我正在阅读关于的Python文档,并且遇到了我不确定的这一段:

Derived classes may override methods of their base classes. 派生类可以覆盖其基类的方法。 Because methods have no special privileges when calling other methods of the same object, a method of a base class that calls another method defined in the same base class may end up calling a method of a derived class that overrides it. 因为方法在调用同一对象的其他方法时没有特殊权限,所以调用同一基类中定义的另一个方法的基类方法最终可能会调用覆盖它的派生类的方法。 (For C++ programmers: all methods in Python are effectively virtual.) (对于C ++程序员:Python中的所有方法都是虚拟的。)

Example: 例:

class A:
    def foo(self):
        self.bar()

    def bar(self):
        print "from A"

class B(A):
    def foo(self):
        self.bar()

    def bar(self):
        print "from B"

Does this mean that an object of class A obj = A() can somehow end up printing "from B"? 这是否意味着A类obj = A()的对象可以某种方式最终打印“从B”? Am I reading this correctly? 我读得对吗? I apologize if this doesn't make sense. 如果这没有意义,我道歉。 I'm a bit confused as to how python handles Inheritance and overriding. 关于python如何处理继承和覆盖,我有点困惑。 Thanks! 谢谢!

No. There's no way the superclass can know anything about the subclass. 没有。超类无法知道关于子类的任何信息。 What it means is if you instantiate the subclass B, and it inherits a method foo() , and overrides a method bar() , then when you call foo() , that will call the bar() definition in B, not the bar() definition in A. This is not what the superclass writer intended - he expected his call to bar() to go to his own definition. 这意味着如果你实例化子类B,并且它继承了一个方法foo() ,并覆盖一个方法bar() ,那么当你调用foo()时,它将调用B中的bar()定义,而不是bar() A中的bar()定义。这不是超类作者的意图 - 他希望他对bar()调用能够达到他自己的定义。

No, it means that you if you have following object: 不,如果您有以下对象,则表示您:

class B(A):
    def bar(self):
        print "from B"

and you do 你也是

obj = B()
obj.foo()

then this will print from B as foo() , which is defined in the base class , calls bar() , which is also defined in the base class, but overridden in the derived class . 然后这from B打印为foo() ,它在基类中定义,调用bar() ,它也在基类中定义,但在派生类中被重写

At least this is how I read it. 至少这是我读它的方式。

a = A()
a.foo()
b = B()
b.foo()
a.bar = b.bar
a.foo()

output: 输出:

from A
from B
from B

My answer doesn't necessarily contradict the ones posted already, but it does show a way to get the base class to print "from B" by calling the base class method from the inherited class. 我的答案不一定与已发布的答案相矛盾,但它确实显示了一种方法,通过从继承的类调用基类方法来使基类打印“从B”。 The base class still calls the inherited class method as it is working from the inherited self. 基类仍然调用继承的类方法,因为它是从继承的self中工作的。 Perhaps this is the type of situation the paragraph is referring to? 也许这是段落所指的那种情况?

class A:
    def foo(self):
        self.bar()

    def bar(self):
        print("from A")

class B(A):
    def foo(self):
        super().foo()

    def bar(self):
        print("from B")


A().foo() #prints "from A"
B().foo() #prints "from B" but indirectly through the base class
class A:
    def f(self):
        print 'a.f'
        self.g()

    def g(self):
        print 'a.g'

class B(A):
    def g(self):
        print 'b.g'

b = B()
b.f()

# a.f
# b.g

No, any object that is an A will invoke A.bar and print " from A " 不,任何作为A对象都会调用A.bar并打印“ from A

Which overridden method is called depends on what the object is , not what other classes may be derived from its class. 调用哪个重写方法取决于对象什么,而不是从其类派生的其他类。 Think of the class as a cookie cutter, and the object as the cookie. 将类视为cookie切割器,将对象视为cookie。

Not exactly: 不完全是:

class A:
   def foo(self):
       self.bar()

   def foo2(self):
       self.bar2()

   def bar(self):
       print "Bar A"

   def bar2(self):
       print "Bar2 A"

class B(A):
   def bar(self):
       print "Bar B"

objA = A()
objA.foo()
objA.foo2()

objB = B()
objB.foo()
objB.foo2()

Output: 输出:

Bar A
Bar2 A
Bar B
Bar2 A

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

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