Python多重继承

Ano*_*oop 5 python

我有3个A,B和D类,如下所示

class A(object):
    def test(self):
        print "called A"

class B(object):
    def test(self):
        print "called B"

class D(A,B):
    def test(self):
        super(A,self).test()

inst_d=D()
inst_d.test()

----------------------------------------
Output:
  called B
Run Code Online (Sandbox Code Playgroud)

问题:在D.test(),我在打电话super(A,self).test().B.test()即使该方法A.test()也存在,为什么只被调用?

Dan*_*man 6

因为你告诉它不要.在D.test你告诉它调用A 的父母的测试方法- 那是什么super.

通常,您希望在super调用中使用当前的类名.

  • 答案有点不清楚 - "B"不是"A"的父级,因此调用"B.test()"的原因并不明显. (2认同)