2024-04-19 21:08:44 发布
网友
如何调用特定基类上的方法?我知道我可以在下面的例子中使用super(C, self)来获得自动的方法解析-但是我想能够指定我调用哪个基类的方法?在
super(C, self)
class A(object): def test(self): print 'A' class B(object): def test(self): print 'B' class C(A,B): def test(self): print 'C'
只需命名“基类”。在
如果您想从您的C类调用sayB.test:
C
B.test
class C(A,B): def test(self): B.test(self)
示例:
输出:
$ python -i foo.py B >>>
参见:Python Classes (Tutorial)
只需命名“基类”。在
如果您想从您的
C
类调用sayB.test
:示例:
^{pr2}$输出:
参见:Python Classes (Tutorial)
相关问题 更多 >
编程相关推荐