浅析Java内部类在GUI设计中的作用(1)

来源:互联网 发布:虚拟光区mac 编辑:程序博客网 时间:2024/05/21 08:14

对于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

感觉写的不错,收藏一下,希望对需要的朋友有帮助
原创粉丝点击