2014-05-23 73 views
1

如何在特定的基類上調用方法?我知道我可以在下面的例子中使用super(C, self)來獲得自動方法解析 - 但我希望能夠指定我正在調用哪個基類的方法?如何在Python中調用特定基類的方法?

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' 
+0

我假設你想知道如何爲'C'類做這個?你想在運行時區分一個實例嗎?你能展示你想如何使用這個功能嗎? – SethMMorton

+0

@SethMMorton:其他人沒有任何基類可以調用 – Casebash

+0

是的,我意識到這一點。我正在提示你想要什麼的更多細節。 – SethMMorton

回答

4

只是命名「基類」。

如果你想打電話說B.testC類:

class C(A,B): 
    def test(self): 
     B.test(self) 

例子:

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() 

輸出:

$ python -i foo.py 
B 
>>> 

See:Python Classes (Tutorial)