您的位置:首页 > 编程语言 > Python开发

Python 子类调用父类的构造函数

2012-09-27 20:56 162 查看
from:http://www.xinotes.org/notes/note/517/

For old-style classes:
BaseClassName.methodname(self, arguments)


>>> class A:
...     def hello(self):
...         print 'a'
...
>>> A().hello()
a
>>> class C(A):
...     def hello(self):
...         A.hello(self)
...         print 'c'
...
>>> C().hello()
a
c

For new-style class (any class which inherits from [code]object
):
super(ClassName, self).method(args)
[/code]

>>> class M(object):
...     def hello(self):
...         print 'm'
...
>>> class N(M):
...     def hello(self):
...         super(N, self).hello()
...         print 'n'
...
>>> N().hello()
m
n
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: