1 内部类

image.png

  1. class TestInnerClass{
  2. public static void main( String[] args ){
  3. Parcel p = new Parcel();
  4. p.testShip();
  5. Parcel.Contents c = p.new Contents(33); // 类型定义需要用 Parcel, 但后面 new 的时候不需要
  6. Parcel.Destination d = p.new Destination( "Hawii" ); // 还需要注意 new 前面的 p.
  7. Parcel.Contents cc = new Parcel().new Contents(44); // ok,内部类的 new 前面一定要有一个对象实例
  8. p.setProperty( c, d );
  9. p.ship();
  10. }
  11. }
  12. class Parcel {
  13. private Contents c;
  14. private Destination d;
  15. class Contents {
  16. private int i;
  17. Contents( int i ){ this.i = i; }
  18. int value() { return i; }
  19. }
  20. class Destination {
  21. private String label;
  22. Destination(String whereTo) {label = whereTo;}
  23. String readLabel() { return label; }
  24. }
  25. void setProperty( Contents c, Destination d ){
  26. this.c =c; this.d = d;
  27. }
  28. void ship(){
  29. System.out.println( "move "+ c.value() +" to "+ d.readLabel() );
  30. }
  31. public void testShip() {
  32. c = new Contents(22);
  33. d = new Destination("Beijing");
  34. ship();
  35. }
  36. }
  • 内部类可以直接访问外部类的字段和方法,即使是 **private** 的
  • 如果内部类中有与外部类同名的字段或名字,可以使用 OuterClass.this.method() 访问外部类的方法和字段


内部类的修饰符

  • 访问控制符public , protected , 默认 , private
    • 注意: 外部类只能使用 public 或者 默认 进行修饰
  • final , abstract
  • static 修饰符

image.png

  1. class TestInnerStatic {
  2. public static void main(String[] args) {
  3. A.B a_b = new A().new B(); // ok
  4. A a = new A();
  5. A.B ab = a.new B();
  6. Outer.Inner oi = new Outer.Inner(); // 这里 new 前面不需要对象实例
  7. //Outer.Inner oi2 = Outer.new Inner(); //!!!error
  8. //Outer.Inner oi3 = new Outer().new Inner(); //!!! error
  9. }
  10. }
  11. class A {
  12. private int x;
  13. void m(){
  14. new B();
  15. }
  16. static void sm(){
  17. //new B(); // error!!!!
  18. }
  19. class B {
  20. B(){ x=5; }
  21. }
  22. }
  23. class Outer {
  24. static class Inner {
  25. }
  26. }

2 局部类

在类的方法(Java 中所有的方法都定义在类中)中定义的类,称为“方法中的局部类”。
image.png

  1. class TestInnerInMethod {
  2. public static void main(String[] args) {
  3. Object obj = new Outer().makeTheInner(47);
  4. System.out.println("Hello World!" + obj.toString() );
  5. }
  6. }
  7. class Outer {
  8. private int size = 5;
  9. public Object makeTheInner( int localVar ) {
  10. final int finalLocalVar = 99;
  11. class Inner {
  12. public String toString() {
  13. return ( " InnerSize: " + size +
  14. // " localVar: " + localVar + // Error!
  15. " finalLocalVar: " + finalLocalVar
  16. );
  17. }
  18. }
  19. return new Inner();
  20. }
  21. }

3 匿名类

匿名类( anonymous class ) 是一种特殊的内部类。

  • 没有类名,在定义类的时候同时生成一个实例(相当于定义 + new
  • 一次性使用”的类

image.png

  1. class TestInnerAnonymous
  2. {
  3. public static void main(String[] args)
  4. {
  5. Object obj = new Outer().makeTheInner(47);
  6. System.out.println("Hello World!" + obj.toString() );
  7. }
  8. }
  9. class Outer
  10. {
  11. private int size = 5;
  12. public Object makeTheInner( int localVar )
  13. {
  14. final int finalLocalVar = 99;
  15. return new Object() {
  16. public String toString() {
  17. return ( " InnerSize: " + size +
  18. " finalLocalVar: " + finalLocalVar
  19. );
  20. }
  21. };
  22. }
  23. }