日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

組合接口時(shí)的方法沖突

 鳳舞天煌 2007-11-12

===========================組合接口時(shí)的方法沖突========================
在實(shí)現(xiàn)多重繼承時(shí),可能會(huì)碰到一個(gè)小陷阱:當(dāng)一個(gè)類同時(shí)implements 接口1與接口2時(shí),假如接口1與接口2同時(shí)都有一個(gè)相同方法簽名(即方法的名字,參數(shù),不包括返回類型)的方法時(shí),如果在類中沒有再定義該方法,編譯器就會(huì)報(bào)錯(cuò)!
看個(gè)例子:
interface I1 { void f(); }
interface I2 { int f(int i); }
interface I3 { int f(); }
class C { public int f() { return 1; } }

class C2 implements I1, I2 {
     public void f() {}
     public int f(int i) { return 1; } // overloaded
}

class C3 extends C implements I2 {
     public int f(int i) { return 1; } // overloaded
}

class C4 extends C implements I3 {
     // Identical, no problem:
     public int f() { return 1; }
}

// Methods differ only by return type:
//! class C5 extends C implements I1 {}     //標(biāo)記1
//! interface I4 extends I1, I3 {} ///:~     //標(biāo)記2


在標(biāo)記1的地方會(huì)報(bào)錯(cuò)是因?yàn)椋篊 與 I1 擁有相同方法簽名的方法,即 f(),雖然它們的返回類型不同,但是在java的重載中,通過返回類型是不能區(qū)分的,只有方法簽名不同的方法,才可以區(qū)分。由于C5沒有重寫f(),當(dāng)調(diào)用C5的f()時(shí),無法分清到底是要調(diào)用C 還是 I1 的f()。因此會(huì)報(bào)錯(cuò)。
     ----解決方法是 在C5類中,重新定義f()!

要標(biāo)記2的地方也會(huì)報(bào)錯(cuò),道理同上?。?!


===========================組合接口時(shí)的域沖突========================
關(guān)于接口,首先我們想到的是abstract class,因?yàn)榻涌诳梢钥闯?,方法全都是abstract的“類”,關(guān)于abstract 類的特點(diǎn),這里就不多講,書上都講得很清楚,其中有一點(diǎn)很重要的是:一個(gè)繼承于abstract的類,如果被聲明為abstract,那么這個(gè)子類可以不用定義基類中的abstract方法。否則,必須寫出abstract基類中的全部abstract方法!
     正如,我們剛才說的,接口就是一個(gè)abstract類,所以,如果一個(gè)abstract類implements 一個(gè)接口,該類可以不用為該接口的方法做定義,否則也必須對(duì)所有方法都提供具體的實(shí)現(xiàn)!

這里,我們討論的是:組合接口時(shí)的域沖突
看一個(gè)很具有代表性的例子:
abstract class MyClass Implements Interface1, Interface2{  
public void f(){}  
public vlid g(){}  
}  
interface Interface1{  
int VAL_A=1;  
int VAL_B=2;  
void f();  
void g();  
}  
interface Interface2{  
int VAL_B=3;  
inf VAL_C=4;  
void g();  
void h();  
}
Select the one right answer.  
(a) Interface1 and Interface2 do not match, therefore MyClass cannot implement them both.  
(b) MyClass only implements Interface1. Implementation for void h() from Interface2 is missing.  
(c) The declarations of void g() in the two interfaces clash.  
(d) The definitions of int VAL_B in the two interface class.  
(e) Nothing is wrong with the code, it will compile without errors
     手動(dòng)編譯這個(gè)類,發(fā)現(xiàn)可以通過編譯,而且不會(huì)報(bào)錯(cuò)。那么是不是答案就應(yīng)該選擇(e)了呢??
在看答案之前,我們要知道的是:MyClass是abstract的,所以,即使它Implements Interface1, Interface2,也可以不用定義這兩個(gè)接口中的方法!
然后我們先來看選項(xiàng)(a):a 是錯(cuò)的,因?yàn)殡m然方法g()同存于Interface1, Interface2,這可能會(huì)造成
組合接口時(shí)的方法沖突,但MyClass重新對(duì)g()做了定義!所以這樣是可以的

選頂(b)也是錯(cuò)的,因?yàn)镸yClass是abstract的,實(shí)現(xiàn)了Interface2,可以不有定義Interface2中的方法

選頂(C)錯(cuò)誤碼的地方同選項(xiàng)a差不多!

現(xiàn)在只剩下(d)與(e)兩個(gè)選項(xiàng),只有(d)才是正確的,所以本題答案是:d(驚訝吧!)
     本程序雖然可以通過編譯,但如果編譯MyClass.VAL_B時(shí),就會(huì)有錯(cuò)誤,因?yàn)?,這里出現(xiàn)了組合接口時(shí)的域沖突!Interface1, Interface2都有域int VAL_B ,MyClass 實(shí)現(xiàn)了這兩個(gè)類,那么,這個(gè)域也被帶到了該類中,如果調(diào)用VAL_B時(shí),系統(tǒng)不知道要調(diào)用哪個(gè)(類似方法沖突)。

      解決的方法是:在MyClass中再定義一個(gè)static int VAL_B

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多