新聞中心
對(duì)于Java內(nèi)部類(lèi),大家實(shí)際上了解不多。在這里我們以實(shí)際代碼的形式,為大家詳細(xì)介紹Java內(nèi)部類(lèi)在GUI設(shè)計(jì)的作用。

成都創(chuàng)新互聯(lián)是專(zhuān)業(yè)的黔江網(wǎng)站建設(shè)公司,黔江接單;提供網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作,網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專(zhuān)業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行黔江網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專(zhuān)業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專(zhuān)業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
Java內(nèi)部類(lèi)其實(shí)在J2EE編程中使用較少,不過(guò)在窗口應(yīng)用編程中特別常見(jiàn),主要用來(lái)事件的處理。其實(shí),做非GUI編程,內(nèi)部類(lèi)完全可以不用。
內(nèi)部類(lèi)的聲明、訪(fǎng)問(wèn)控制等于外部類(lèi)有所不同,要靈活使用內(nèi)部類(lèi)來(lái)編寫(xiě)程序,還是有相當(dāng)難度的,Java發(fā)明了這種難懂的玩意兒,在其他語(yǔ)言中是沒(méi)有的,但是在Java中,內(nèi)部類(lèi)也相當(dāng)?shù)闹匾绕渥鯣UI開(kāi)發(fā)時(shí)候,事件的響應(yīng)處理全靠?jī)?nèi)部類(lèi)了。
內(nèi)部類(lèi)所做的功能使用外部類(lèi)也同樣可以實(shí)現(xiàn),只是有時(shí)候內(nèi)部類(lèi)做的更巧妙些。
內(nèi)部類(lèi)按照其所在位置不同,可分為以下幾種:
1、(普通的)內(nèi)部類(lèi)(最常見(jiàn)的內(nèi)部類(lèi),內(nèi)部類(lèi)的定義與類(lèi)成員平級(jí),)
2、方法內(nèi)部類(lèi)
3、匿名類(lèi)
4、靜態(tài)內(nèi)部類(lèi)
5、接口內(nèi)部類(lèi)
一、內(nèi)部類(lèi)聲明與訪(fǎng)問(wèn)
1、內(nèi)部類(lèi)直接在類(lèi)的內(nèi)部進(jìn)行聲明??梢月暶鳛閜rivate、protected、public或者默認(rèn)訪(fǎng)問(wèn)權(quán)限,這個(gè)訪(fǎng)問(wèn)權(quán)限約定和外部類(lèi)完全一樣。
2、內(nèi)部類(lèi)自動(dòng)擁有對(duì)其外圍類(lèi)所有成員(方法、屬性)的訪(fǎng)問(wèn)權(quán)。如果內(nèi)部類(lèi)和外部類(lèi)成員的名字完全相同,在內(nèi)部類(lèi)方法中要訪(fǎng)問(wèn)外部類(lèi)成員,則需要使用下面的方式來(lái)訪(fǎng)問(wèn):外部類(lèi)名.this.外部成員名,例如Outer.this.i++; (看例子)
3、必須使用外部類(lèi)對(duì)象來(lái)創(chuàng)建內(nèi)部類(lèi)對(duì)象,而不是直接去new一個(gè)。
格式為:外部對(duì)象名.new 內(nèi)部類(lèi)構(gòu)造方法
比如要?jiǎng)?chuàng)建一個(gè)內(nèi)部類(lèi)iner對(duì)象,需要這么做:
- Outer outer = new Outer();
- Outer.Inner iner = outer.new Inner();
- /**
- * 內(nèi)部類(lèi)創(chuàng)建與初始化
- *
- * @author leizhimin 2009-7-17 13:51:52
- */
- public class Outer {
- private int i = 10;
- private int y = 8;
- Outer() {
- System.out.println("調(diào)用Outer構(gòu)造方法:outer");
- }
- public void sayMsg() {
- System.out.println("Outer class!");
- }
- class Inner {
- int i = 1000;
- Inner() {
- System.out.println("調(diào)用Inner構(gòu)造方法:inner");
- }
- void innerMsg() {
- System.out.println(">>>>>Inner class!");
- sayMsg();
- //訪(fǎng)問(wèn)內(nèi)部類(lèi)自己的成員i,也可以寫(xiě)成 this.i++
- this.i++;
- //訪(fǎng)問(wèn)外部類(lèi)的成員 i和y
- Outer.this.i++;
- y--;
- }
- int getI() {
- return i;
- }
- }
- public void test() {
- Inner in = new Inner();
- in.innerMsg();
- }
- public int getI() {
- return i;
- }
- public void setI(int i) {
- this.i = i;
- }
- }
- class Test1 {
- public static void main(String[] args) {
- Outer outer = new Outer();
- outer.test();
- System.out.println(outer.getI());
- System.out.println("-------1--------");
- Outer.Inner iner = outer.new Inner();
- iner.innerMsg();
- System.out.println(iner.getI());
- System.out.println("-------2--------");
- System.out.println(outer.getI());
- }
- }
運(yùn)行結(jié)果:
調(diào)用Outer構(gòu)造方法:outer
調(diào)用Inner構(gòu)造方法:inner
- >>>>>Inner class!
- Outer class!
- 11
- -------1--------
調(diào)用Inner構(gòu)造方法:inner
- >>>>>Inner class!
- Outer class!
- 1001
- -------2--------
- 12
- Process finished with exit code 0
二、內(nèi)部類(lèi)與接口
1、內(nèi)部類(lèi)可以實(shí)現(xiàn)接口。
2、內(nèi)部類(lèi)之間相互可見(jiàn),但并非內(nèi)部類(lèi)之間方法都可見(jiàn)。
- public interface Foo{
- void say();
- }
- public interface Bar {
- void readme();
- }
- /**
- * 內(nèi)部類(lèi)實(shí)現(xiàn)接口
- *
- * @author leizhimin 2009-7-17 14:57:50
- */
- public class Test2 {
- public static void main(String[] args) {
- Outer outer = new Outer();
- Foo f = outer.genFoo();
- Bar b = outer.genBar();
- f.say();
- b.readme();
- }
- }
- class Outer {
- private class FooImpl implements Foo {
- public void say() {
- System.out.println("say foo!");
- }
- }
- private class BarImpl implements Bar {
- public void readme() {
- System.out.println("say bar!");
- }
- }
- public Foo genFoo() {
- return new FooImpl();
- }
- public Bar genBar() {
- return new BarImpl();
- }
- }
輸入結(jié)果:
say foo!
say bar!
Process finished with exit code 0
三、訪(fǎng)問(wèn)權(quán)限
外部類(lèi)分兩種:
一種嵌入了內(nèi)部類(lèi)聲明代碼外部類(lèi),稱(chēng)為直接外部類(lèi)。 另一種是與內(nèi)部類(lèi)沒(méi)有任何關(guān)系的外部類(lèi),稱(chēng)為外部類(lèi)。
在同一個(gè)直接外部類(lèi)中,內(nèi)部類(lèi)之間所有的方法都是相互可見(jiàn)的,包含在直接外部類(lèi)的main()中可見(jiàn)。
在外部類(lèi)中,要看到一個(gè)類(lèi)的內(nèi)部類(lèi)成員,則至少要求這個(gè)內(nèi)部類(lèi)的class和成員權(quán)限大于或等于protected。
- /**
- * 內(nèi)部類(lèi)實(shí)現(xiàn)接口
- *
- * @author leizhimin 2009-7-17 14:57:50
- */
- public class Test2 {
- public static void main(String[] args) {
- Outer o = new Outer();
- Outer.Bar b = o.genBar();
- b.readme();
- }
- }
- class Outer {
- protected class Foo {
- protected void say() {
- System.out.println("say foo!");
- }
- private void test() {
- System.out.println("----test------");
- }
- }
- protected class Bar {
- protected void readme() {
- System.out.println("say bar!");
- new Foo().test();
- }
- }
- public Foo genFoo() {
- return new Foo();
- }
- public Bar genBar() {
- return new Bar();
- }
- }
#p#
四、方法內(nèi)部類(lèi)
方法內(nèi)部類(lèi)只在該方法內(nèi)部可見(jiàn),方法內(nèi)部類(lèi)可以定義在方法中的任何位置。
- /**
- * 內(nèi)部類(lèi)實(shí)現(xiàn)接口
- *
- * @author leizhimin 2009-7-17 14:57:50
- */
- public class Test2 {
- public static void main(String[] args) {
- Outer outer = new Outer();
- Foo f = outer.genFoo();
- Bar b = outer.genBar();
- f.say();
- b.readme();
- }
- }
- class Outer {
- public Foo genFoo() {
- //方法內(nèi)的內(nèi)部類(lèi)
- class FooImpl implements Foo {
- public void say() {
- System.out.println("say foo!");
- }
- }
- return new FooImpl();
- }
- public Bar genBar() {
- Bar b = null;
- if (true) {
- //任意位置的內(nèi)部類(lèi)
- class BarImpl implements Bar {
- public void readme() {
- System.out.println("say bar!");
- }
- }
- b = new BarImpl();
- }
- return b;
- }
- }
運(yùn)行結(jié)果:
say foo!
say bar!
Process finished with exit code 0
五、匿名類(lèi)
匿名類(lèi)不給出類(lèi)名,直接定義一個(gè)類(lèi),通常這個(gè)類(lèi)實(shí)現(xiàn)了某種接口或者抽象。匿名類(lèi)的訪(fǎng)問(wèn)權(quán)限更沒(méi)有討論價(jià)值了,看個(gè)例子就行了。
在一些多線(xiàn)程程序中比較常見(jiàn),有點(diǎn)變態(tài),呵呵。
- /**
- * 匿名類(lèi).
- *
- * @author leizhimin 2009-7-17 15:56:17
- */
- public class Test3 {
- public Foo f = new Foo() {
- public void say() {
- System.out.println("O(∩_∩)O哈哈~!");
- }
- };
- public Foo test() {
- return new Foo() {
- public void say() {
- System.out.println("say foo!");
- }
- };
- }
- public static void main(String[] args) {
- Test3 t = new Test3();
- t.f.say();
- t.test().say();
- }
- }
- interface Foo {
- void say();
- }
運(yùn)行結(jié)果:
say foo!
- Process finished with exit code 0
- /**
- * 普通類(lèi)的匿名初始化
- *
- * @author leizhimin 2009-7-17 16:13:31
- */
- public class Fk {
- private String x;
- public Fk(String x) {
- this.x = x;
- }
- @Override
- public String toString() {
- return "Fk{" +
- "x='" + x + '\'' +
- '}';
- }
- }
- class Test4 {
- public Fk hehe() {
- //把后面的一對(duì)大括號(hào)去掉呢,呵呵
- return new Fk("fk") {
- };
- }
- public static void main(String[] args) {
- Test4 t = new Test4();
- Fk f = t.hehe();
- System.out.println(f);
- }
- }
運(yùn)行結(jié)果:
Fk{x='fk'}
Process finished with exit code 0
還有一個(gè)不得不提的經(jīng)典實(shí)例,來(lái)自thining in java,有改動(dòng):
- interface Service {
- void method1();
- void method2();
- }
- interface ServiceFactory {
- Service getService();
- }
- class Implementation1 implements Service {
- private Implementation1() {}
- public void method1() {System.out.println("Implementation1 method1");}
- public void method2() {System.out.println("Implementation1 method2");}
- public static ServiceFactory factory = new ServiceFactory() {
- public Service getService() {
- return new Implementation1();
- }
- };
- }
- class Implementation2 implements Service {
- private Implementation2() {}
- public void method1() {System.out.println("Implementation2 method1");}
- public void method2() {System.out.println("Implementation2 method2");}
- public static ServiceFactory factory = new ServiceFactory() {
- public Service getService() {
- return new Implementation2();
- }
- };
- }
- public class Factories {
- public static void serviceConsumer(ServiceFactory fact) {
- Service s = fact.getService();
- s.method1();
- s.method2();
- }
- public static void main(String[] args) {
- serviceConsumer(Implementation1.factory);
- serviceConsumer(Implementation2.factory);
- }
- }
這個(gè)應(yīng)用給了我們很多思考,我就不說(shuō)了,不同人看了會(huì)有不同的感受。
內(nèi)部類(lèi)的巧妙使用會(huì)讓你的代碼很牛,如果要形容下,那就是:沒(méi)看懂的時(shí)候感覺(jué)神出鬼沒(méi),看懂后感覺(jué)鬼斧神工。不過(guò)這些代碼多了,別人想看懂都難,想看懂你思路就難上加難了。呵呵!
六、靜態(tài)內(nèi)部類(lèi)
靜態(tài)內(nèi)部類(lèi)是static class型的內(nèi)部類(lèi),這種內(nèi)部類(lèi)特點(diǎn)是:它不能訪(fǎng)問(wèn)外部類(lèi)的非靜態(tài)成員。要?jiǎng)?chuàng)建靜態(tài)內(nèi)部類(lèi)對(duì)象時(shí)候,也不需要外部類(lèi)對(duì)象了,直接可以:
new 外部類(lèi)名.內(nèi)部類(lèi)構(gòu)造方法
來(lái)創(chuàng)建,給個(gè)例子:
- /**
- * 靜態(tài)內(nèi)部類(lèi)
- *
- * @author leizhimin 2009-7-17 16:53:05
- */
- public class Outer {
- public static int i =500;
- protected static class Inner {
- int i =100;
- String name;
- Inner(String name) {
- this.name = name;
- }
- void sayHello() {
- System.out.println("Hello " + name);
- Outer.i++;
- }
- }
- public Inner genInner(String name) {
- return new Inner(name);
- }
- }
- class Test {
- public static void main(String[] args) {
- Outer.Inner in1 = new Outer.Inner("1111");
- in1.sayHello();
- System.out.println(Outer.i);
- Outer.Inner in2 = new Outer().genInner("2222");
- in2.sayHello();
- System.out.println(Outer.i);
- }
- }
運(yùn)行結(jié)果:
Hello 1111
501
Hello 2222
502
Process finished with exit code 0
七、接口內(nèi)部類(lèi)
接口內(nèi)部類(lèi)自動(dòng)都是public static的,相當(dāng)于為接口定義了一種變量類(lèi)型,這在java的設(shè)計(jì)中就有使用,比如在HashMap中,就有:
static class Entry
下面我給個(gè)例子,
- /**
- * 接口內(nèi)部類(lèi)
- *
- * @author leizhimin 2009-7-17 17:20:28
- */
- public interface AInterface {
- void readme();
- class Inner1 implements AInterface {
- public void readme() {
- System.out.println("我是一個(gè)接口內(nèi)部類(lèi)");
- }
- }
- }
- class Main {
- public static void main(String[] args) {
- AInterface.Inner1 in1 = new AInterface.Inner1();
- in1.readme();
- }
- }
八、內(nèi)部的類(lèi)的嵌套
所謂內(nèi)部類(lèi)嵌套,就是內(nèi)部類(lèi)里面再定義內(nèi)部類(lèi)。其實(shí)這種用法還真沒(méi)見(jiàn)過(guò),試試寫(xiě)個(gè)簡(jiǎn)單例子看看吧:
- /**
- * 嵌套內(nèi)部類(lèi)
- *
- * @author leizhimin 2009-7-17 17:33:48
- */
- public class Outer {
- private void f0() {
- System.out.println("f0");
- }
- class A {
- private void a() {
- f0();
- System.out.println("a");
- }
- class B {
- protected void b() {
- a();
- System.out.println("b");
- }
- }
- }
- }
- class Test{
- public static void main(String[] args) {
- Outer o = new Outer();
- Outer.A a = o.new A();
- Outer.A.B b = a.new B();
- b.b();
- }
- }
運(yùn)行結(jié)果:
f0
a
b
Process finished with exit code 0
八、內(nèi)部類(lèi)的繼承
內(nèi)部類(lèi)的繼承,可以繼承內(nèi)部類(lèi),也可以繼承外部類(lèi)。
- /**
- * 內(nèi)部類(lèi)的繼承,可以繼承內(nèi)部類(lèi),也可以繼承外部類(lèi)
- *
- * @author leizhimin 2009-7-22 13:50:01
- */
- public class Outer {
- class Inner {
- void doSomething() {
- System.out.println("Inner doing ...");
- }
- }
- class Inner2 extends Inner {
- void doSomething() {
- System.out.println("Inner2 doing ...");
- }
- void readme() {
- System.out.println("HeHe!");
- }
- }
- }
- class Test {
- public static void main(String[] args) {
- Outer outer = new Outer();
- Outer.Inner in = outer.new Inner();
- Outer.Inner2 in2 = outer.new Inner2();
- in.doSomething();
- in2.doSomething();
- in2.readme();
- }
- }
運(yùn)行結(jié)果:
Inner doing ...
Inner2 doing ...
HeHe!
Process finished with exit code 0
總結(jié):
內(nèi)部類(lèi)是Java中最復(fù)雜深?yuàn)W的概念之一,而且內(nèi)部類(lèi)在訪(fǎng)問(wèn)控制,修飾符,繼承,實(shí)現(xiàn),抽象,序列化等等很多方面都是一個(gè)很讓人迷惑的問(wèn)題,在實(shí)際中,這些問(wèn)題也許永遠(yuǎn)沒(méi)機(jī)會(huì)沒(méi)時(shí)間搞清,但是一般說(shuō)來(lái),懂得以上的內(nèi)部類(lèi)的知識(shí)就足夠用了。
內(nèi)部類(lèi)的設(shè)計(jì)也許是彌補(bǔ)Java語(yǔ)言本身的先天不足吧,作為語(yǔ)言來(lái)說(shuō),這個(gè)特性太變態(tài)了點(diǎn),難道就沒(méi)別的法了?
以上的總結(jié)完全是建立在實(shí)踐基礎(chǔ)上的,所列舉的例子也許偏頗,不能全面反映問(wèn)題的本質(zhì),希望有興趣的博友多多發(fā)表自己的看法與觀(guān)
當(dāng)前文章:淺析Java內(nèi)部類(lèi)在GUI設(shè)計(jì)中的作用
URL分享:http://m.5511xx.com/article/djpsish.html


咨詢(xún)
建站咨詢(xún)
