3

我试图理解 Python 中的类继承并创建了以下继承,

问题:当我继承B类并调用方法A时,它会打印“Im方法B”..它不会调用A类的方法A吗?

class A(object):
    def methodA(self):
        print 'Im method A'


class B(A):
    def methodA(self):
        print 'Im method B'    

class C(A):
    def methodA(self):
        print 'Im method C'    

class D(B,C):
    def methodA(self):
        print 'Im method D'            
def main():
    x = D()
    x.methodA()
4

2 回答 2

5

不,如果要调用要覆盖的方法,则必须手动执行super

class B(A):
    def methodA(self):
        super(B, self).methodA()
        print 'Im method B' 
于 2013-03-05T19:29:24.193 回答
1

不,Python 方法默认是隐式虚拟的。这意味着它们将始终被相应的子类方法覆盖。

于 2013-03-05T19:37:05.500 回答