淺析Java內部類在GUI設計中的作用(1)

對於Java內部類,大家實際上了解不多。在這裏我們以實際代碼的形式,爲大家詳細介紹Java內部類在GUI設計的作用。

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

內部類的聲明、訪問控制等於外部類有所不同,要靈活使用內部類來編寫程序,還是有相當難度的,Java發明了這種難懂的玩意兒,在其他語言中是沒有 的,但是在Java中,內部類也相當的重要,尤其做GUI開發時候,事件的響應處理全靠內部類了。

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

內部類按照其所在位置不同,可分爲以下幾種:

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

2、方法內部類

3、匿名類

4、靜態內部類

5、接口內部類

一、內部類聲明與訪問

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

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

3、必須使用外部類對象來創建內部類對象,而不是直接去new一個。

格式爲:外部對象名.new 內部類構造方法

比如要創建一個內部類iner對象,需要這麼做: 

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

運行結果:

調用Outer構造方法:outer

調用Inner構造方法:inner

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

調用Inner構造方法:inner

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

二、內部類與接口

1、內部類可以實現接口。

2、內部類之間相互可見,但並非內部類之間方法都可見。

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

輸入結果:

say foo!

say bar!

Process finished with exit code 0

三、訪問權限

外部類分兩種:

一種嵌入了內部類聲明代碼外部類,稱爲直接外部類。 另一種是與內部類沒有任何關係的外部類,稱爲外部類。

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

在外部類中,要看到一個類的內部類成員,則至少要求這個內部類的class和成員權限大於或等於protected。

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



本問轉自 http://developer.51cto.com/art/201002/183375.htm

感覺寫的不錯,收藏一下,希望對需要的朋友有幫助
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章