1、定義
? 一個(gè)類的定義放在另一個(gè)類的內(nèi)部,這個(gè)類就叫做內(nèi)部類。
- public?class?First?{ ??
- public?class?Contents{ ??
- ????public?void?f(){ ??
- ????System.out.println("In?Class?First's?inner?Class?Contents?method?f()"); ??
- ????} ??
- } ??
- ?}??
?像這樣的,Contents就叫做內(nèi)部類
內(nèi)部類了解外圍類,并能與之通信(后面詳細(xì)講)
2、鏈接到外圍類
? 創(chuàng)建了內(nèi)部類對(duì)象時(shí),它會(huì)與創(chuàng)造它的外圍對(duì)象有了某種聯(lián)系,于是能訪問(wèn)外圍類的所有成員,不需任何特殊條件。?
?
- ???public?class?First?{ ??
- public?class?Contents{ ??
- ?????????public?void?getStr(){ ??
- ????????System.out.println("First.str="+str); ??
- ?????} ??
- } ??
- private?String?str; ??
- ????} ??
- ???
? 那么,它是如何實(shí)現(xiàn)的呢?
? 是這樣的,用外圍類創(chuàng)建內(nèi)部類對(duì)象時(shí),此內(nèi)部類對(duì)象會(huì)秘密的捕獲一個(gè)指向外圍類的引用,于是,可以通過(guò)這個(gè)引用來(lái)訪問(wèn)外圍類的成員。
? 通常,這些都是編譯器來(lái)處理,我們看不到,也不用關(guān)心這個(gè)。
? 正是因?yàn)槿绱耍覀儎?chuàng)建內(nèi)部類對(duì)象時(shí),必須與外圍類對(duì)象相關(guān)聯(lián)。
? 注:嵌套類(后面會(huì)講到)除外。
3、使用關(guān)鍵字.this與.new
? 內(nèi)部類中得到當(dāng)前外圍類對(duì)象的引用,可以使用.this關(guān)鍵字,注意與new的區(qū)別
?
- ????private?int?num?; ??
- public?Test2(){ ??
- ???? ??
- } ??
- ??
- public?Test2(int?num){ ??
- ????this.num?=?num; ??
- } ??
- ??
- private?class?Inner{ ??
- ????public?Test2?getTest2(){ ??
- ????????return?Test2.this; ??
- ????} ??
- ???? ??
- ????public?Test2?newTest2(){ ??
- ????????return?new?Test2(); ??
- ????} ??
- } ??
- ??
- public?static?void?main(String?[]?args){ ??
- ????Test2?test?=?new?Test2(5); ??
- ????Test2.Inner?inner?=?test.new?Inner(); ??
- ????Test2?test2?=?inner.getTest2(); ??
- ????Test2?test3?=?inner.newTest2(); ??
- ????System.out.println(test2.num); ??
- ????System.out.println(test3.num); ??
- } ??
- ???
? 使用.this后,得到時(shí)創(chuàng)建該內(nèi)部類時(shí)使用的外圍類對(duì)象的引用,new則是創(chuàng)建了一個(gè)新的引用。
? .new關(guān)鍵字
? 如果想直接創(chuàng)建一個(gè)內(nèi)部類對(duì)象,而不是通過(guò)外圍類對(duì)象的方法來(lái)得到,可以使用.new關(guān)鍵字
? 形式是這樣的:
?
- OutClass.InnerClass?obj?=?outClassInstance.new?InnerClass(); ??
? 必須是外圍類對(duì)象.new,而不能是外圍類.new
?
- ???public?class?First?{ ??
- public?class?Contents{ ??
- ????public?void?f(){ ??
- ????????System.out.println("In?Class?First's?inner?Class?Contents?method?f()"); ??
- ????} ??
- ????public?void?getStr(){ ??
- ????????System.out.println("First.str="+str); ??
- ????} ??
- } ??
- ??
- public?static?void?main(String?[]?args){ ??
- ????First?first?=?new?First(); ??
- ????First.Contents?contents?=?first.new?Contents(); ??
- ????contents.f(); ??
- } ??
- ????} ??
- ???
? 而且需要注意的是,在創(chuàng)建外圍類對(duì)象之前,不可能創(chuàng)建內(nèi)部類的對(duì)象(嵌套類除外)。
4、內(nèi)部類與向上轉(zhuǎn)型
? 將內(nèi)部類向上轉(zhuǎn)型為基類型,尤其是接口時(shí),內(nèi)部類就有了用武之地。?
?
- ????public?interface?Shape?{ ??
- public?void?paint(); ??
- ????} ??
- ????public?class?Painter?{ ??
- ???? ??
- ???????private?class?InnerShape?implements?Shape{ ??
- ????public?void?paint(){ ??
- ????????System.out.println("painter?paint()?method"); ??
- ????} ??
- } ??
- ??
- public?Shape?getShape(){ ??
- ????return?new?InnerShape(); ??
- }??? ??
- ???? ??
- ???????public?static?void?main(String?[]args){ ??
- ????Painter?painter?=?new?Painter(); ??
- ????Shape?shape?=?painter.?getShape(); ??
- ????shape.paint(); ??
- } ??
- ????} ??
- ???
? 這樣,private內(nèi)部類給累的設(shè)計(jì)者提供了一種途徑,通過(guò)這種方式可以完全阻止任何依賴于類型的編碼,并完全隱藏實(shí)現(xiàn)的細(xì)節(jié)。
5、方法內(nèi)的類
? 可以在方法內(nèi)創(chuàng)建一個(gè)類。
?
- public?void?test(){ ??
- ass?Inner{ ??
- ?public?void?method(){ ??
- ystem.out.println("在方法內(nèi)創(chuàng)建的類"); ??
- ?} ??
- ??
- } ??
? 另外,方法內(nèi)部的類也不是在調(diào)用方法時(shí)才會(huì)創(chuàng)建的,它們一樣也被編譯了(怎么知道的?后面會(huì)有講解)。
6、匿名內(nèi)部類?
?
- ??public?class?Painter?{ ??
- ublic?Shape?getShape(){ ??
- return?new?Shape(){ ??
- ????public?void?paint(){ ??
- ????????System.out.println("painter?paint()?method"); ??
- ????} ??
- }; ??
- ??
- ??????public?static?void?main(String?[]?args){ ??
- ????????????Painter?painter?=?new?Painter(); ??
- ????????????Shape?shape?=?painter.getShape(); ??
- ????????????shape.paint(); ??
- ??????} ??
- ??} ??
- ??public?interface?Shape?{ ??
- ublic?void?paint(); ??
- ??} ??
?? 匿名類,顧名思義,就是沒(méi)有名稱。
? getShape()方法里,就使用了匿名內(nèi)部類。
? 看上去很奇怪,不符合傳統(tǒng)的寫法?
? 第一眼看上去確實(shí)是這樣的。
? 這樣寫,意思是創(chuàng)建了一個(gè)實(shí)現(xiàn)了Shape的匿名類的對(duì)象。
? 匿名類可以創(chuàng)建,接口,抽象類,與普通類的對(duì)象。創(chuàng)建接口時(shí),必須實(shí)現(xiàn)接口中所有方法。
?
? 這是無(wú)參的,如果需要參數(shù)呢?
? 可以直接傳。?
?
- ???public?class?B?{ ??
- public?A?getA(int?num){ ??
- ????return?new?A(num){ ??
- ???????? ??
- ????}; ??
- } ??
- ???} ??
- ???public?class?A?{ ??
- private?int?num; ??
- public?A(int?num){ ??
- ????this.num?=?num; ??
- } ??
- public?A(){ ??
- ???? ??
- } ??
- ???} ??
- ???
? 如果使用到了這個(gè)參數(shù),那么這個(gè)參數(shù)就必須是final的。
?
- ???public?class?B?{ ??
- public?A?getA(final?int?num){ ??
- ????return?new?A(num){ ??
- ???????public?int?getNum(){ ??
- ?????????????????????return?num; ??
- ??????????????????} ??
- ????}; ??
- } ??
- ???} ??
- ???public?class?A?{ ??
- private?int?num; ??
- public?A(int?num){ ??
- ????this.num?=?num; ??
- } ??
- public?A(){ ??
- ???? ??
- } ??
- ???} ??
- ???
? 另外,還可以在匿名內(nèi)部類里定義屬性
? 由于類是匿名的,自然沒(méi)有構(gòu)造器,如果想模仿構(gòu)造器,可以采用實(shí)例初始化({})
?
- ???public?A?getA(){ ??
- return?new?A(){ ??
- ????int?num?=?0; ??
- ????String?str; ??
- ????{ ??
- ????????str?=?"javaeye"; ??
- ????????System.out.println("hello?robbin"); ??
- ????} ??
- }; ??
- ???} ??
- ???
另外可以通過(guò)匿名內(nèi)部類來(lái)改造工廠方法。
- ??public?interface?Service?{ ??
- public?void?method1(); ??
- ??} ??
- ??public?interface?ServiceFactory?{ ??
- Service?getService(); ??
- ??} ??
- ??public?class?Implemention1?implements?Service{ ??
- public?void?method1(){ ??
- ????System.out.println("In?Implemention1?method?method1()"); ??
- } ??
- ??
- public?static?ServiceFactory?factory?=?new?ServiceFactory(){ ??
- ????public?Service?getService(){ ??
- ????????return?new?Implemention1(); ??
- ????} ??
- }; ??
- ??} ??
- ??public?class?Implemention2?implements?Service?{ ??
- public?void?method1(){ ??
- ????System.out.println("in?Implemention2?method?method1()"); ??
- } ??
- ??
- public?static?ServiceFactory?factory?=?new?ServiceFactory(){ ??
- ????public?Service?getService(){ ??
- ????????return?new?Implemention2(); ??
- ????} ??
- }; ??
- ??
- ??} ??
- ??public?class?Test?{ ??
- public?static?void?main(String?[]args){ ??
- ????service(Implemention1.factory); ??
- ????service(Implemention2.factory); ??
- ???? ??
- ????ServiceFactory?factory1?=?Implemention1.factory; ??
- ????Service?service1?=?factory1.getService(); ??
- ????service1.method1(); ??
- ???? ??
- ????ServiceFactory?factory2?=?Implemention1.factory; ??
- ????Service?service2?=?factory2.getService(); ??
- ????service2.method1(); ??
- } ??
- ??} ??
在Implemention1和2中匿名內(nèi)部類用在字段初始化地方。
這樣定義的工廠方法,代碼上看起來(lái)是不是優(yōu)雅一些?

7、嵌套類
static的內(nèi)部類就叫做嵌套類
前面提到了很多次,嵌套類是個(gè)例外
使用嵌套類時(shí)有兩點(diǎn)需要注意:
?? a、創(chuàng)建嵌套類對(duì)象時(shí),不需要外圍類
?? b、在嵌套類中,不能像普通內(nèi)部類一樣訪問(wèn)外圍類的非static成員
- ??public?class?StaticClass?{ ??
- private?int?num; ??
- private?static?int?sum?=?2; ??
- private?static?class?StaticInnerClass{ ??
- ????public?int?getNum(){ ??
- ????//只能訪問(wèn)sum,不能訪問(wèn)num ??
- ???????????????return?sum; ??
- ????} ??
- } ??
- ??} ??
- ??public?class?Test?{ ??
- public?static?void?main(String?[]?args){ ??
- ???????????????//可以直接通過(guò)new來(lái)創(chuàng)建嵌套類對(duì)象 ??
- ????StaticClass.StaticInnerClass?inner?=?new?StaticClass.StaticInnerClass(); ??
- ????inner.getNum(); ??
- } ??
- ??} ??
? 8、內(nèi)部類標(biāo)識(shí)符
? 我們知道每個(gè)類會(huì)產(chǎn)生一個(gè).class文件,文件名即為類名
? 同樣,內(nèi)部類也會(huì)產(chǎn)生這么一個(gè).class文件,但是它的名稱卻不是內(nèi)部類的類名,而是有著嚴(yán)格的限制:外圍類的名字,加上$,再加上內(nèi)部類名字。
? 前面說(shuō)到得定義在方法內(nèi)的內(nèi)部類,不是在調(diào)用方法時(shí)生成,而是與外圍類一同編譯,就可以通過(guò)查看.class文件的方式來(lái)證明。
? 9、為何要內(nèi)部類?
??? a、內(nèi)部類提供了某種進(jìn)入外圍類的窗戶。
??? b、也是最吸引人的原因,每個(gè)內(nèi)部類都能獨(dú)立地繼承一個(gè)接口,而無(wú)論外圍類是否已經(jīng)繼承了某個(gè)接口。
? 因此,內(nèi)部類使多重繼承的解決方案變得更加完整。
? 在項(xiàng)目中,需要多重繼承,如果是兩個(gè)接口,那么好辦,接口支持多重繼承。
? 如果是兩個(gè)類呢?這時(shí)只有使用內(nèi)部類了。
?
- ???public?interface?One?{ ??
- public?void?inOne(); ??
- ???} ??
- ???public?interface?Two?{ ??
- public?void?inTwo(); ??
- ???} ??
- ???//兩個(gè)接口,用普通類就可實(shí)現(xiàn)多重繼承 ??
- ???public?class?CommonClass?implements?One,Two?{ ??
- public?void?inOne(){ ??
- ????System.out.println("CommonClass?inOne()?method"); ??
- } ??
- ??
- public?void?inTwo(){ ??
- ????System.out.println("CommonClass?inTwo()?method"); ??
- } ??
- ???} ??
- ???public?abstract?class?Three?{ ??
- public?abstract?void?inThree(); ??
- ???} ??
- ???public?abstract?class?Four?{ ??
- public?abstract?void?inFour(); ??
- ???} ??
- ???//兩個(gè)抽象類,使用普通類無(wú)法實(shí)現(xiàn)多重繼承 ??
- ??? ??
- ???//使用內(nèi)部類可以實(shí)現(xiàn) ??
- ???public?class?Contents?extends?Three?{ ??
- public?void?inThree(){ ??
- ????System.out.println("In?Contents?inThress()?method"); ??
- } ??
- ??
- public?class?InnerFour?extends?Four{ ??
- ????public?void?inFour(){ ??
- ????????System.out.println("In?Contents"); ??
- ????} ??
- ???? ??
- } ??
- ???} ??
- ???
? 另外,還有好多地方可以使用內(nèi)部類。讀過(guò)hibernate源代碼的同學(xué),應(yīng)該可以發(fā)現(xiàn),里面有好多內(nèi)部類。
? 最常見(jiàn)的內(nèi)部類,應(yīng)該是Map.Entry了,可以看看源代碼~?
總結(jié):
? 內(nèi)部類的特性大致就是上述了,特性很直觀,了解了之后,使用也很簡(jiǎn)單。
? 但是,何時(shí)使用我說(shuō)的并不是很明確,因?yàn)楸救酥R(shí)有限,使用內(nèi)部類也不是很多。項(xiàng)目中很少用,好像就是ActiveMQ那里用了一些。
? 不過(guò),相信大家在了解了內(nèi)部類的特性之后,再隨著時(shí)間的推移,慢慢積累經(jīng)驗(yàn),應(yīng)該會(huì)做出自己的判斷,會(huì)在何時(shí)使用內(nèi)部類,怎樣應(yīng)用了。

Author: orangelizq
email: orangelizq@163.com
|
|
歡迎大家訪問(wèn)我的個(gè)人網(wǎng)站 萌萌的IT人