成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

開發 后端
本文將為大家介紹Java內部類,內部類主要用于GUI設計方面,平時大家可能接觸不多,但了解內部類還是很有的。

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

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

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

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

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

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

2、方法內部類

3、匿名類

4、靜態內部類

5、接口內部類

一、內部類聲明與訪問

1、內部類直接在類的內部進行聲明??梢月暶鳛閜rivate、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.         }   
  40. }  

#p#

四、方法內部類

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

  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 outer = new Outer();   
  9.                 Foo f = outer.genFoo();   
  10.                 Bar b = outer.genBar();   
  11.                 f.say();   
  12.                 b.readme();   
  13.         }   
  14. }   
  15.  
  16. class Outer {   
  17.         public Foo genFoo() {   
  18.                 //方法內的內部類   
  19.                 class FooImpl implements Foo {   
  20.                         public void say() {   
  21.                                 System.out.println("say foo!");   
  22.                         }   
  23.                 }   
  24.                 return new FooImpl();   
  25.         }   
  26.  
  27.         public Bar genBar() {   
  28.                 Bar b = null;   
  29.                 if (true) {   
  30.                         //任意位置的內部類   
  31.                         class BarImpl implements Bar {   
  32.                                 public void readme() {   
  33.                                         System.out.println("say bar!");   
  34.                                 }   
  35.                         }   
  36.                         b = new BarImpl();   
  37.                 }   
  38.                 return b;   
  39.         }   
  40. }  

運行結果:

say foo!

say bar!

Process finished with exit code 0

五、匿名類

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

在一些多線程程序中比較常見,有點變態,呵呵。

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

運行結果:

say foo!

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

運行結果:

Fk{x='fk'}

Process finished with exit code 0

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

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

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

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

六、靜態內部類

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

new 外部類名.內部類構造方法

來創建,給個例子:

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

運行結果:

Hello 1111

501

Hello 2222

502

Process finished with exit code 0

七、接口內部類

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

static class Entry<K,V> implements Map.Entry<K,V>

下面我給個例子,

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

八、內部的類的嵌套

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

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

運行結果:

f0

a

b

Process finished with exit code 0

八、內部類的繼承

內部類的繼承,可以繼承內部類,也可以繼承外部類。

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

運行結果:

Inner doing ...

Inner2 doing ...

HeHe!

Process finished with exit code 0

總結

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

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

以上的總結完全是建立在實踐基礎上的,所列舉的例子也許偏頗,不能全面反映問題的本質,希望有興趣的博友多多發表自己的看法與觀點。

【編輯推薦】

  1. Java 7的***類函數:學習閉包的使用
  2. Scala的類層級:與Java類之異同
  3. Java教程 Java類中的包
  4. Java類基礎
  5. 解析Java類和對象的初始化過程
責任編輯:彭凡 來源: javaeye
相關推薦

2009-06-11 13:08:29

Java內部類Java編程思想

2020-01-15 11:14:21

Java算法排序

2011-07-21 15:44:33

Java內部類

2020-12-14 10:23:23

Java內部類外部類

2011-03-29 14:11:15

內部類

2023-10-19 13:24:00

Java工具

2025-04-29 08:09:39

2023-03-06 07:53:36

JavaN種內部類

2015-12-08 09:05:41

Java內部類

2009-07-29 09:18:49

Java內部類

2011-12-06 11:12:59

Java

2009-07-01 09:17:36

對象比較Java

2009-12-24 14:30:56

VB.NET

2010-08-26 10:41:45

C#內部類

2009-06-01 08:48:19

作用域變量作用域對象作用域

2019-12-23 14:32:38

Java內部類代碼

2022-06-07 08:31:44

JavaUnsafe

2023-09-07 11:43:10

2013-08-06 09:35:20

視覺設計UI設計設計

2011-01-05 09:14:38

Delphi XE
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 久久久久久91 | 日韩黄色免费 | 三级av在线 | 国产一级视频在线 | 午夜精品久久久久久久99黑人 | 中文字幕视频在线看 | 久久岛国| 一区二区三区视频在线免费观看 | www.精品国产| 日韩精品一区二区三区中文在线 | 福利片在线 | 精品免费看| 亚洲电影一区 | 日本久久久影视 | 国产成年人视频 | 欧美99| 天天搞天天操 | av在线黄 | 亚洲精品视频在线观看视频 | 久久1区 | 日日爱视频| 国产精品69av | 一二三四在线视频观看社区 | 精品在线看 | 亚洲精品一区二区三区蜜桃久 | 精品国产一区二区三区久久久四川 | 国产黄色网 | 国产亚洲精品综合一区 | 亚洲欧美在线视频 | 狠狠操电影 | 日韩av福利在线观看 | 一区二区视屏 | 天天拍天天操 | 精久久久| 国产精品毛片无码 | 久久综合九九 | 久久久久久久一区 | 国产日韩欧美一区二区 | 视频1区2区 | 91亚洲免费| 欧美在线天堂 |