Python类继承新类型类

3

我正在尝试理解Python中的类继承,在下面的继承中进行了创建:

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

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()
2个回答

5
不,如果你想调用你所覆盖的方法,你需要手动使用 super
class B(A):
    def methodA(self):
        super(B, self).methodA()
        print 'Im method B' 

1

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接