Python-方法重载的问题

定义一个父类,在写一个子类继承他,重载他的foo方法:

class Father:
    def foo(self):
        print"I am father"

class Son(Father):
    def foo(self):
        print"I am son"

son=Son()
son.foo()   

运行结果:

//结果
I am son

但是我们想使用父类的foo怎么办呢,按以下方式就行了,父类名.被重载的方法(这里传入子类对象)

Father.foo(son)

结果:

//结果
I am father

猜你喜欢

转载自wjy320.iteye.com/blog/1999830