Java基础之内部类

刚在一个叫51cto的网站看到的,感觉这哥们总结的比较全了,例子都很简单,都能说明问题。收藏,呵呵。

还有就是集合里用的迭代器也是内部类的设计,我也就知道这一个,哈哈。

以下为原文:

对于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.         }   
  40. }

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

  1.  

    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

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值