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

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

四、方法内部类

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

  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语言本身的先天不足吧,作为语言来说,这个特性太变态了点,难道就没别的法了?

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

 

本文转自 http://developer.51cto.com/art/201002/183375_1.htm

希望对有需要的朋友有帮助

原创粉丝点击