Python论坛  - 讨论区

标题:[python-chinese] how to call parent's method

2006年10月06日 星期五 18:01

cry zyqmail在tom.com
星期五 十月 6 18:01:54 HKT 2006

Liming_Do,您好!

That's it. Thanks.

在 2006-10-06 16:08:00 您写道:
>使用内置函数super(type,obj)
> 
>class A(object):
> def meth(self,arg):
>  print "A's meth - " + arg
> 
>class B(A):
> def meth(self,arg):
>  super(B,self).meth(arg)
>  print "B's meth - " + arg
> 
>>>>a = A()
>>>>b = B()
>>>>a.meth('a')
>A's meth - a
>>>>b.meth('b')
>A's meth - b
>B's meth - b
>
>-----Original Message-----
>From: python-chinese-bounces在lists.python.cn [mailto:python-chinese-bounces在lists.python.cn]On Behalf Of Leo Jay
>Sent: Friday, October 06, 2006 3:32 PM
>To: python-chinese在lists.python.cn
>Subject: Re: [python-chinese] how to call parent's method
>
>
>On 10/6/06, cry < zyqmail在tom.com> wrote: 
>
>python,您好!
>
>我从一个class,继承出一个subclass,override一个method,如何才能从我自己的method中去调用parent class中同名的method呢?
>
>
>如果要用到的话,为什么要用一样的名字呢?
> 
>可以这样:
>>>> class A:
>....     def test(self):
>....             print 'test a'
>....
>>>> class B(A):
>....     def test(self):
>....             print 'test b'
>....     def t(self):
>....             A.test(self)
>....             self.test()
>....
>>>> b = B()
>>>> b.t()
>test a
>test b
>>>>
>
>
>
>-- 
>Best Regards,
>Leo Jay 
>_______________________________________________
>python-chinese
>Post: send python-chinese在lists.python.cn
>Subscribe: send subscribe to python-chinese-request在lists.python.cn
>Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
>Detail Info: http://python.cn/mailman/listinfo/python-chinese

                    致
礼!

            cry
            zyqmail在tom.com



[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号