日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
淺析Java內(nèi)部類在GUI設(shè)計中的作用

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

成都創(chuàng)新互聯(lián)是專業(yè)的黔江網(wǎng)站建設(shè)公司,黔江接單;提供網(wǎng)站設(shè)計、成都網(wǎng)站制作,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行黔江網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!

Java內(nèi)部類其實在J2EE編程中使用較少,不過在窗口應(yīng)用編程中特別常見,主要用來事件的處理。其實,做非GUI編程,內(nèi)部類完全可以不用。

內(nèi)部類的聲明、訪問控制等于外部類有所不同,要靈活使用內(nèi)部類來編寫程序,還是有相當難度的,Java發(fā)明了這種難懂的玩意兒,在其他語言中是沒有的,但是在Java中,內(nèi)部類也相當?shù)闹匾?,尤其做GUI開發(fā)時候,事件的響應(yīng)處理全靠內(nèi)部類了。

內(nèi)部類所做的功能使用外部類也同樣可以實現(xiàn),只是有時候內(nèi)部類做的更巧妙些。

內(nèi)部類按照其所在位置不同,可分為以下幾種:

1、(普通的)內(nèi)部類(最常見的內(nèi)部類,內(nèi)部類的定義與類成員平級,)

2、方法內(nèi)部類

3、匿名類

4、靜態(tài)內(nèi)部類

5、接口內(nèi)部類

一、內(nèi)部類聲明與訪問

1、內(nèi)部類直接在類的內(nèi)部進行聲明。可以聲明為private、protected、public或者默認訪問權(quán)限,這個訪問權(quán)限約定和外部類完全一樣。

2、內(nèi)部類自動擁有對其外圍類所有成員(方法、屬性)的訪問權(quán)。如果內(nèi)部類和外部類成員的名字完全相同,在內(nèi)部類方法中要訪問外部類成員,則需要使用下面的方式來訪問:外部類名.this.外部成員名,例如Outer.this.i++;  (看例子)

3、必須使用外部類對象來創(chuàng)建內(nèi)部類對象,而不是直接去new一個。

格式為:外部對象名.new 內(nèi)部類構(gòu)造方法

比如要創(chuàng)建一個內(nèi)部類iner對象,需要這么做:

 
 
 
  1.  Outer outer = new Outer(); 
  2.         Outer.Inner iner = outer.new Inner(); 
  3. /** 
  4. * 內(nèi)部類創(chuàng)建與初始化 
  5. * @author leizhimin 2009-7-17 13:51:52 
  6. */ 
  7. public class Outer { 
  8.         private int i = 10; 
  9.         private int y = 8; 
  10.         Outer() { 
  11.                 System.out.println("調(diào)用Outer構(gòu)造方法:outer"); 
  12.         } 
  13.         public void sayMsg() { 
  14.                 System.out.println("Outer class!"); 
  15.         } 
  16.         class Inner { 
  17.                 int i = 1000; 
  18.                 Inner() { 
  19.                         System.out.println("調(diào)用Inner構(gòu)造方法:inner"); 
  20.                 } 
  21.                 void innerMsg() { 
  22.                         System.out.println(">>>>>Inner class!"); 
  23.                         sayMsg(); 
  24.                         //訪問內(nèi)部類自己的成員i,也可以寫成 this.i++ 
  25.                         this.i++; 
  26.                         //訪問外部類的成員 i和y 
  27.                         Outer.this.i++; 
  28.                         y--; 
  29.                 } 
  30.                 int getI() { 
  31.                         return i; 
  32.                 } 
  33.         } 
  34.         public void test() { 
  35.                 Inner in = new Inner(); 
  36.                 in.innerMsg(); 
  37.         } 
  38.         public int getI() { 
  39.                 return i; 
  40.         } 
  41.         public void setI(int i) { 
  42.                 this.i = i; 
  43.         } 
  44. class Test1 { 
  45.         public static void main(String[] args) { 
  46.                 Outer outer = new Outer(); 
  47.                 outer.test(); 
  48.                 System.out.println(outer.getI()); 
  49.                 System.out.println("-------1--------"); 
  50.                 Outer.Inner iner = outer.new Inner(); 
  51.                 iner.innerMsg(); 
  52.                 System.out.println(iner.getI()); 
  53.                 System.out.println("-------2--------"); 
  54.                 System.out.println(outer.getI()); 
  55.         } 

運行結(jié)果:

調(diào)用Outer構(gòu)造方法:outer

調(diào)用Inner構(gòu)造方法:inner

 
 
 
  1. >>>>>Inner class! 
  2. Outer class! 
  3. 11 
  4. -------1-------- 

調(diào)用Inner構(gòu)造方法:inner

 
 
 
  1. >>>>>Inner class! 
  2. Outer class! 
  3. 1001 
  4. -------2-------- 
  5. 12 
  6. Process finished with exit code 0 

二、內(nèi)部類與接口

1、內(nèi)部類可以實現(xiàn)接口。

2、內(nèi)部類之間相互可見,但并非內(nèi)部類之間方法都可見。

 
 
 
  1. public interface Foo{ 
  2.          void say(); 
  3. public interface Bar { 
  4.         void readme(); 
  5. /** 
  6. * 內(nèi)部類實現(xiàn)接口 
  7. * @author leizhimin 2009-7-17 14:57:50 
  8. */ 
  9. public class Test2 { 
  10.         public static void main(String[] args) { 
  11.                 Outer outer = new Outer(); 
  12.                 Foo f = outer.genFoo(); 
  13.                 Bar b = outer.genBar(); 
  14.                 f.say(); 
  15.                 b.readme(); 
  16.         } 
  17. class Outer { 
  18.         private class FooImpl implements Foo { 
  19.                 public void say() { 
  20.                         System.out.println("say foo!"); 
  21.                 } 
  22.         } 
  23.         private class BarImpl implements Bar { 
  24.                 public void readme() { 
  25.                         System.out.println("say bar!"); 
  26.                 } 
  27.         } 
  28.         public Foo genFoo() { 
  29.                 return new FooImpl(); 
  30.         } 
  31.         public Bar genBar() { 
  32.                 return new BarImpl(); 
  33.         } 

輸入結(jié)果:

say foo!

say bar!

Process finished with exit code 0

三、訪問權(quán)限

外部類分兩種:

一種嵌入了內(nèi)部類聲明代碼外部類,稱為直接外部類。 另一種是與內(nèi)部類沒有任何關(guān)系的外部類,稱為外部類。

在同一個直接外部類中,內(nèi)部類之間所有的方法都是相互可見的,包含在直接外部類的main()中可見。

在外部類中,要看到一個類的內(nèi)部類成員,則至少要求這個內(nèi)部類的class和成員權(quán)限大于或等于protected。

 
 
 
  1. /** 
  2. * 內(nèi)部類實現(xiàn)接口 
  3. * @author leizhimin 2009-7-17 14:57:50 
  4. */ 
  5. public class Test2 { 
  6.         public static void main(String[] args) { 
  7.                 Outer o = new Outer(); 
  8.                 Outer.Bar b = o.genBar(); 
  9.                 b.readme(); 
  10.         } 
  11. class Outer { 
  12.         protected class Foo { 
  13.                 protected void say() { 
  14.                         System.out.println("say foo!"); 
  15.                 } 
  16.                 private void test() { 
  17.                         System.out.println("----test------"); 
  18.                 } 
  19.         } 
  20.         protected class Bar { 
  21.                 protected void readme() { 
  22.                         System.out.println("say bar!"); 
  23.                         new Foo().test(); 
  24.                 } 
  25.         } 
  26.         public Foo genFoo() { 
  27.                 return new Foo(); 
  28.         } 
  29.         public Bar genBar() { 
  30.                 return new Bar(); 
  31.         } 

#p#

四、方法內(nèi)部類

方法內(nèi)部類只在該方法內(nèi)部可見,方法內(nèi)部類可以定義在方法中的任何位置。

 
 
 
  1. /** 
  2. * 內(nèi)部類實現(xiàn)接口 
  3. * @author leizhimin 2009-7-17 14:57:50 
  4. */ 
  5. public class Test2 { 
  6.         public static void main(String[] args) { 
  7.                 Outer outer = new Outer(); 
  8.                 Foo f = outer.genFoo(); 
  9.                 Bar b = outer.genBar(); 
  10.                 f.say(); 
  11.                 b.readme(); 
  12.         } 
  13. class Outer { 
  14.         public Foo genFoo() { 
  15.                 //方法內(nèi)的內(nèi)部類 
  16.                 class FooImpl implements Foo { 
  17.                         public void say() { 
  18.                                 System.out.println("say foo!"); 
  19.                         } 
  20.                 } 
  21.                 return new FooImpl(); 
  22.         } 
  23.         public Bar genBar() { 
  24.                 Bar b = null; 
  25.                 if (true) { 
  26.                         //任意位置的內(nèi)部類 
  27.                         class BarImpl implements Bar { 
  28.                                 public void readme() { 
  29.                                         System.out.println("say bar!"); 
  30.                                 } 
  31.                         } 
  32.                         b = new BarImpl(); 
  33.                 } 
  34.                 return b; 
  35.         } 

運行結(jié)果:

say foo!

say bar!

Process finished with exit code 0

五、匿名類

匿名類不給出類名,直接定義一個類,通常這個類實現(xiàn)了某種接口或者抽象。匿名類的訪問權(quán)限更沒有討論價值了,看個例子就行了。

在一些多線程程序中比較常見,有點變態(tài),呵呵。

 
 
 
  1. /** 
  2. * 匿名類. 
  3. * @author leizhimin 2009-7-17 15:56:17 
  4. */ 
  5. public class Test3 { 
  6.         public Foo f = new Foo() { 
  7.                 public void say() { 
  8.                         System.out.println("O(∩_∩)O哈哈~!"); 
  9.                 } 
  10.         }; 
  11.         public Foo test() { 
  12.                 return new Foo() { 
  13.                         public void say() { 
  14.                                 System.out.println("say foo!"); 
  15.                         } 
  16.                 }; 
  17.         } 
  18.         public static void main(String[] args) { 
  19.                 Test3 t = new Test3(); 
  20.                 t.f.say(); 
  21.                 t.test().say(); 
  22.         } 
  23. interface Foo { 
  24.         void say(); 

運行結(jié)果:

say foo!

 
 
 
  1. Process finished with exit code 0 
  2. /** 
  3. * 普通類的匿名初始化 
  4. * @author leizhimin 2009-7-17 16:13:31 
  5. */ 
  6. public class Fk { 
  7.         private String x; 
  8.         public Fk(String x) { 
  9.                 this.x = x; 
  10.         } 
  11.         @Override 
  12.         public String toString() { 
  13.                 return "Fk{" + 
  14.                                 "x='" + x + '\'' + 
  15.                                 '}'; 
  16.         } 
  17. class Test4 { 
  18.         public Fk hehe() { 
  19.                 //把后面的一對大括號去掉呢,呵呵 
  20.                 return new Fk("fk") { 
  21.                 }; 
  22.         } 
  23.         public static void main(String[] args) { 
  24.                 Test4 t = new Test4(); 
  25.                 Fk f = t.hehe(); 
  26.                 System.out.println(f); 
  27.         } 

運行結(jié)果:

Fk{x='fk'}

Process finished with exit code 0

還有一個不得不提的經(jīng)典實例,來自thining in java,有改動:

 
 
 
  1. interface Service { 
  2.     void method1(); 
  3.     void method2(); 
  4. interface ServiceFactory { 
  5.     Service getService(); 
  6. class Implementation1 implements Service { 
  7.     private Implementation1() {} 
  8.     public void method1() {System.out.println("Implementation1 method1");} 
  9.     public void method2() {System.out.println("Implementation1 method2");} 
  10.     public static ServiceFactory factory = new ServiceFactory() { 
  11.             public Service getService() { 
  12.                 return new Implementation1(); 
  13.             } 
  14.         }; 
  15. class Implementation2 implements Service { 
  16.     private Implementation2() {} 
  17.     public void method1() {System.out.println("Implementation2 method1");} 
  18.     public void method2() {System.out.println("Implementation2 method2");} 
  19.     public static ServiceFactory factory = new ServiceFactory() { 
  20.             public Service getService() { 
  21.                 return new Implementation2(); 
  22.             } 
  23.         }; 
  24. public class Factories { 
  25.     public static void serviceConsumer(ServiceFactory fact) { 
  26.         Service s = fact.getService(); 
  27.         s.method1(); 
  28.         s.method2(); 
  29.     } 
  30.     public static void main(String[] args) { 
  31.         serviceConsumer(Implementation1.factory); 
  32.         serviceConsumer(Implementation2.factory); 
  33.     } 

這個應(yīng)用給了我們很多思考,我就不說了,不同人看了會有不同的感受。

內(nèi)部類的巧妙使用會讓你的代碼很牛,如果要形容下,那就是:沒看懂的時候感覺神出鬼沒,看懂后感覺鬼斧神工。不過這些代碼多了,別人想看懂都難,想看懂你思路就難上加難了。呵呵!

六、靜態(tài)內(nèi)部類

靜態(tài)內(nèi)部類是static class型的內(nèi)部類,這種內(nèi)部類特點是:它不能訪問外部類的非靜態(tài)成員。要創(chuàng)建靜態(tài)內(nèi)部類對象時候,也不需要外部類對象了,直接可以:

new 外部類名.內(nèi)部類構(gòu)造方法

來創(chuàng)建,給個例子:

 
 
 
  1. /** 
  2. * 靜態(tài)內(nèi)部類 
  3. * @author leizhimin 2009-7-17 16:53:05 
  4. */ 
  5. public class Outer { 
  6.         public static int i =500; 
  7.         protected static class Inner { 
  8.                 int i =100; 
  9.                 String name; 
  10.                 Inner(String name) { 
  11.                         this.name = name; 
  12.                 } 
  13.                 void sayHello() { 
  14.                         System.out.println("Hello " + name); 
  15.                         Outer.i++; 
  16.                 } 
  17.         } 
  18.         public Inner genInner(String name) { 
  19.                 return new Inner(name); 
  20.         } 
  21. class Test { 
  22.         public static void main(String[] args) { 
  23.                 Outer.Inner in1 = new Outer.Inner("1111"); 
  24.                 in1.sayHello(); 
  25.                 System.out.println(Outer.i); 
  26.                 Outer.Inner in2 = new Outer().genInner("2222"); 
  27.                 in2.sayHello(); 
  28.                 System.out.println(Outer.i); 
  29.         } 

運行結(jié)果:

Hello 1111

501

Hello 2222

502

Process finished with exit code 0

七、接口內(nèi)部類

接口內(nèi)部類自動都是public static的,相當于為接口定義了一種變量類型,這在java的設(shè)計中就有使用,比如在HashMap中,就有:

static class Entry implements Map.Entry

下面我給個例子,

 
 
 
  1. /** 
  2. * 接口內(nèi)部類 
  3. * @author leizhimin 2009-7-17 17:20:28 
  4. */ 
  5. public interface AInterface { 
  6.         void readme(); 
  7.         class Inner1 implements AInterface { 
  8.                 public void readme() { 
  9.                         System.out.println("我是一個接口內(nèi)部類"); 
  10.                 } 
  11.         } 
  12. class Main { 
  13.         public static void main(String[] args) { 
  14.                 AInterface.Inner1 in1 = new AInterface.Inner1(); 
  15.                 in1.readme(); 
  16.         } 

八、內(nèi)部的類的嵌套

所謂內(nèi)部類嵌套,就是內(nèi)部類里面再定義內(nèi)部類。其實這種用法還真沒見過,試試寫個簡單例子看看吧:

 
 
 
  1. /** 
  2. * 嵌套內(nèi)部類 
  3. * @author leizhimin 2009-7-17 17:33:48 
  4. */ 
  5. public class Outer { 
  6.         private void f0() { 
  7.                 System.out.println("f0"); 
  8.         } 
  9.         class A { 
  10.                 private void a() { 
  11.                         f0(); 
  12.                         System.out.println("a"); 
  13.                 } 
  14.                 class B { 
  15.                         protected void b() { 
  16.                                 a(); 
  17.                                 System.out.println("b"); 
  18.                         } 
  19.                 } 
  20.         } 
  21. class Test{ 
  22.         public static void main(String[] args) { 
  23.                 Outer o = new Outer(); 
  24.                 Outer.A    a =     o.new A(); 
  25.                 Outer.A.B b = a.new B(); 
  26.                 b.b(); 
  27.         } 

運行結(jié)果:

f0

a

b

Process finished with exit code 0

八、內(nèi)部類的繼承

內(nèi)部類的繼承,可以繼承內(nèi)部類,也可以繼承外部類。

 
 
 
  1. /** 
  2. * 內(nèi)部類的繼承,可以繼承內(nèi)部類,也可以繼承外部類 
  3. * @author leizhimin 2009-7-22 13:50:01 
  4. */ 
  5. public class Outer { 
  6.         class Inner { 
  7.                 void doSomething() { 
  8.                         System.out.println("Inner doing ..."); 
  9.                 } 
  10.         } 
  11.         class Inner2 extends Inner { 
  12.                 void doSomething() { 
  13.                         System.out.println("Inner2 doing ..."); 
  14.                 } 
  15.                 void readme() { 
  16.                         System.out.println("HeHe!"); 
  17.                 } 
  18.         } 
  19. class Test { 
  20.         public static void main(String[] args) { 
  21.                 Outer outer = new Outer(); 
  22.                 Outer.Inner in = outer.new Inner(); 
  23.                 Outer.Inner2 in2 = outer.new Inner2(); 
  24.                 in.doSomething(); 
  25.                 in2.doSomething(); 
  26.                 in2.readme(); 
  27.         } 

運行結(jié)果:

Inner doing ...

Inner2 doing ...

HeHe!

Process finished with exit code 0

總結(jié)

內(nèi)部類是Java中最復雜深奧的概念之一,而且內(nèi)部類在訪問控制,修飾符,繼承,實現(xiàn),抽象,序列化等等很多方面都是一個很讓人迷惑的問題,在實際中,這些問題也許永遠沒機會沒時間搞清,但是一般說來,懂得以上的內(nèi)部類的知識就足夠用了。

內(nèi)部類的設(shè)計也許是彌補Java語言本身的先天不足吧,作為語言來說,這個特性太變態(tài)了點,難道就沒別的法了?

以上的總結(jié)完全是建立在實踐基礎(chǔ)上的,所列舉的例子也許偏頗,不能全面反映問題的本質(zhì),希望有興趣的博友多多發(fā)表自己的看法與觀
本文名稱:淺析Java內(nèi)部類在GUI設(shè)計中的作用
URL地址:http://www.5511xx.com/article/djpsish.html