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

对于Java内部类,大家实际上了解不多。在这里我们以实际代码的形式,为大家详细介绍Java内部类在GUI设计的作用。

成都创新互联是专业的黔江网站建设公司,黔江接单;提供网站设计、成都网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行黔江网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

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. * @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("调用Outer构造方法: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("调用Inner构造方法:inner"); 
  20.                 } 
  21.                 void innerMsg() { 
  22.                         System.out.println(">>>>>Inner class!"); 
  23.                         sayMsg(); 
  24.                         //访问内部类自己的成员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.         } 

运行结果:

调用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. Process finished with exit code 0 

二、内部类与接口

1、内部类可以实现接口。

2、内部类之间相互可见,但并非内部类之间方法都可见。

 
 
 
  1. public interface Foo{ 
  2.          void say(); 
  3. public interface Bar { 
  4.         void readme(); 
  5. /** 
  6. * 内部类实现接口 
  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.         } 

输入结果:

say foo!

say bar!

Process finished with exit code 0

三、访问权限

外部类分两种:

一种嵌入了内部类声明代码外部类,称为直接外部类。 另一种是与内部类没有任何关系的外部类,称为外部类。

在同一个直接外部类中,内部类之间所有的方法都是相互可见的,包含在直接外部类的main()中可见。

在外部类中,要看到一个类的内部类成员,则至少要求这个内部类的class和成员权限大于或等于protected。

 
 
 
  1. /** 
  2. * 内部类实现接口 
  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#

四、方法内部类

方法内部类只在该方法内部可见,方法内部类可以定义在方法中的任何位置。

 
 
 
  1. /** 
  2. * 内部类实现接口 
  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.                 //方法内的内部类 
  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.                         //任意位置的内部类 
  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.         } 

运行结果:

say foo!

say bar!

Process finished with exit code 0

五、匿名类

匿名类不给出类名,直接定义一个类,通常这个类实现了某种接口或者抽象。匿名类的访问权限更没有讨论价值了,看个例子就行了。

在一些多线程程序中比较常见,有点变态,呵呵。

 
 
 
  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(); 

运行结果:

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.         } 

运行结果:

Fk{x='fk'}

Process finished with exit code 0

还有一个不得不提的经典实例,来自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.     } 

这个应用给了我们很多思考,我就不说了,不同人看了会有不同的感受。

内部类的巧妙使用会让你的代码很牛,如果要形容下,那就是:没看懂的时候感觉神出鬼没,看懂后感觉鬼斧神工。不过这些代码多了,别人想看懂都难,想看懂你思路就难上加难了。呵呵!

六、静态内部类

静态内部类是static class型的内部类,这种内部类特点是:它不能访问外部类的非静态成员。要创建静态内部类对象时候,也不需要外部类对象了,直接可以:

new 外部类名.内部类构造方法

来创建,给个例子:

 
 
 
  1. /** 
  2. * 静态内部类 
  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.         } 

运行结果:

Hello 1111

501

Hello 2222

502

Process finished with exit code 0

七、接口内部类

接口内部类自动都是public static的,相当于为接口定义了一种变量类型,这在java的设计中就有使用,比如在HashMap中,就有:

static class Entry implements Map.Entry

下面我给个例子,

 
 
 
  1. /** 
  2. * 接口内部类 
  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("我是一个接口内部类"); 
  10.                 } 
  11.         } 
  12. class Main { 
  13.         public static void main(String[] args) { 
  14.                 AInterface.Inner1 in1 = new AInterface.Inner1(); 
  15.                 in1.readme(); 
  16.         } 

八、内部的类的嵌套

所谓内部类嵌套,就是内部类里面再定义内部类。其实这种用法还真没见过,试试写个简单例子看看吧:

 
 
 
  1. /** 
  2. * 嵌套内部类 
  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.         } 

运行结果:

f0

a

b

Process finished with exit code 0

八、内部类的继承

内部类的继承,可以继承内部类,也可以继承外部类。

 
 
 
  1. /** 
  2. * 内部类的继承,可以继承内部类,也可以继承外部类 
  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.         } 

运行结果:

Inner doing ...

Inner2 doing ...

HeHe!

Process finished with exit code 0

总结

内部类是Java中最复杂深奥的概念之一,而且内部类在访问控制,修饰符,继承,实现,抽象,序列化等等很多方面都是一个很让人迷惑的问题,在实际中,这些问题也许永远没机会没时间搞清,但是一般说来,懂得以上的内部类的知识就足够用了。

内部类的设计也许是弥补Java语言本身的先天不足吧,作为语言来说,这个特性太变态了点,难道就没别的法了?

以上的总结完全是建立在实践基础上的,所列举的例子也许偏颇,不能全面反映问题的本质,希望有兴趣的博友多多发表自己的看法与观

分享标题:浅析Java内部类在GUI设计中的作用
文章转载:http://www.hantingmc.com/qtweb/news11/279861.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联