Cas*_*ash 2 python inheritance
如何在特定基类上调用方法?我知道我可以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'
Run Code Online (Sandbox Code Playgroud)
只需命名“基类”。
如果你想B.test从你的C班级打电话说:
class C(A,B):
def test(self):
B.test(self)
Run Code Online (Sandbox Code Playgroud)
例子:
class A(object):
def test(self):
print 'A'
class B(object):
def test(self):
print 'B'
class C(A, B):
def test(self):
B.test(self)
c = C()
c.test()
Run Code Online (Sandbox Code Playgroud)
输出:
$ python -i foo.py
B
>>>
Run Code Online (Sandbox Code Playgroud)
请参阅:Python 类(教程)