`
dasheng
  • 浏览: 146296 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

<java编程思想>学习笔记10,第十章 内部类

阅读更多

1,已经将一个类的定义放在另一个类的定义内部,这就是内部类。

 

2,内部类可以访问其外围类的方法和字段。

 

3,.this和.new

如果你需要生成对外部类对象的引用,可以使用外部类的名字后面紧跟圆点和this。

 

public class DotThis {
  void f() { System.out.println("DotThis.f()"); }
  public class Inner {
    public DotThis outer() {
     
return DotThis.this;
      // A plain "this" would be Inner's "this"
    }
  }
  public Inner inner() { return new Inner(); }
  public static void main(String[] args) {
    DotThis dt = new DotThis();
    DotThis.Inner dti = dt.inner();
    dti.outer().f();
  }
} /* Output:

 

有时要告知某些其他对象,去创建某个内部类的对象。你必须在new表达式中提供对其他外部类对象的引用。这是需要使

 

用.new语法。

 

public class DotNew {
  public class Inner {}
  public static void main(String[] args) {
    DotNew dn = new DotNew();
    DotNew.Inner dni = dn.new Inner();
  }
} ///:~

 

4,在方法和作用域内的内部类

 

可以在一个方法里面或者任意的作用域内定义内部类。有以下的理由:

 

   1,你实现了某类型的接口,于是可以创建并返回对其的引用;

   2,你需要解决一个复杂的问题,想创建一类来辅助你的解决方案,但是又不希望这个类是公共可用的。

 

public class Parcel5 {
 
public Destination destination(String s) {
    class PDestination implements Destination {
      private String label;
      private PDestination(String whereTo) {
        label = whereTo;
      }
      public String readLabel() { return label; }
    }
    return new PDestination(s);
  }
  public static void main(String[] args) {
    Parcel5 p = new Parcel5();
    Destination d = p.destination("Tasmania");
  }
} ///:~

5,匿名内部类

 

public class Parcel7 {
  public Contents contents() {
    return new Contents() { // Insert a class definition
      private int i = 11;
      public int value() { return i; }
    }; // Semicolon required in this case
  }
  public static void main(String[] args) {
    Parcel7 p = new Parcel7();
    Contents c = p.contents();
  }
} ///:~

 

contents()方法将返回值的生成与表示这个返回值的类的定义结合在一起,这个类是匿名的,没有名字的。

 

6,嵌套类

 

如果不需要内部类对象与其外围类对象之间有联系,那么可以将内部类声明为static,这通常叫嵌套类

 

public class Parcel11 {
  private static class ParcelContents implements Contents {
    private int i = 11;
    public int value() { return i; }
  }
  protected static class ParcelDestination
  implements Destination {
    private String label;
    private ParcelDestination(String whereTo) {
      label = whereTo;
    }
    public String readLabel() { return label; } 
    // Nested classes can contain other static elements:
    public static void f() {}
    static int x = 10;
    static class AnotherLevel {
      public static void f() {}
      static int x = 10;
    }
  }
  public static Destination destination(String s) {
    return new ParcelDestination(s);
  }
  public static Contents contents() {
    return new ParcelContents();
  }
  public static void main(String[] args) {
    Contents c = contents();
    Destination d = destination("Tasmania");
  }
} ///:~

7,接口内部的类

 

正常情况下,不能在接口内部放置任何代码,但是嵌套类可以作为接口的一部分。你放到接口中的任何类都自动地是public

 

和static。

 

public interface ClassInInterface {
  void howdy();
  class Test implements ClassInInterface {
    public void howdy() {
      System.out.println("Howdy!");
    }
    public static void main(String[] args) {
      new Test().howdy();
    }
  }
} /* Output:

8,为什么需要内部类

 

为了解决多重继承问题

 

interface A {}
interface B {}

class X implements A, B {}

class Y implements A {
  B makeB() {
    // Anonymous inner class:
    return new B() {};
  }
}

public class MultiInterfaces {
  static void takesA(A a) {}
  static void takesB(B b) {}
  public static void main(String[] args) {
    X x = new X();
    Y y = new Y();
    takesA(x);
    takesA(y);
    takesB(x);
    takesB(y.makeB());
  }
} ///:~

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics