Java内部类

来源:互联网 发布:淘宝女装进货攻略 编辑:程序博客网 时间:2024/06/07 01:26

Java的内部类是个蛮有趣的东西。

在类中定义类,这就是内部类,想定义内部类对象需要先定义外部类的对象,因为内部类需要拿到其外部类对象的引用,所以应该用外部类的对象生成内部类的对象(嵌套类除外),当然这需要使用.this和.new(外部类名.this是外部类的引用,用外部类对象.new生成内部类对象)。

package innerclasses;//thinking in java P193//: innerclasses/DotThis.java// Qualifying access to the outer-class object.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:DotThis.f()*///:~package innerclasses;//thinking in java P193//: innerclasses/DotNew.java// Creating an inner class directly using the .new syntax.public class DotNew {  public class Inner {}  public static void main(String[] args) {    DotNew dn = new DotNew();    DotNew.Inner dni = dn.new Inner();  }} ///:~

将内部类定义为private,然后用public方法来创建并返回内部类对象并将返回值类型进行向上转型是很好的隐藏实现代码细节的机制,尤其是向上转型为一个接口时。在方法和作用域内部的类有其作用范围。


内部类似乎更好玩,一个方法,返回一个对象,在返回对象时(前),定义这个返回对象到底长什么样子,如果基类要是需要参数,那就简单地将参数传递给基类的构造器好了:

package innerclasses;//thinking in java P197//: innerclasses/Parcel8.java// Calling the base-class constructor.public class Parcel8 {public Wrapping wrapping(int x) {// Base constructor call:return new Wrapping(x) { // Pass constructor argument.public int value() {return super.value() * 47;}}; // Semicolon required}public static void main(String[] args) {Parcel8 p = new Parcel8();Wrapping w = p.wrapping(10);}} // /:~
在匿名内部类用到的参数要定义为final类型,但是要是在匿名内部类的基类中使用不要求其是final的。

上篇博客写到工厂接口的方法,这里用工厂内部类的方法:

package innerclasses;//thinking in java P199//: innerclasses/Factories.javaimport static net.mindview.util.Print.*;interface Service {  void method1();  void method2();}interface ServiceFactory {  Service getService();}class Implementation1 implements Service {  private Implementation1() {}  public void method1() {print("Implementation1 method1");}  public void method2() {print("Implementation1 method2");}  public static ServiceFactory factory =    new ServiceFactory() {      public Service getService() {        return new Implementation1();      }    };}class Implementation2 implements Service {  private Implementation2() {}  public void method1() {print("Implementation2 method1");}  public void method2() {print("Implementation2 method2");}  public static ServiceFactory factory =    new ServiceFactory() {      public Service getService() {        return new Implementation2();      }    };}public class Factories {  public static void serviceConsumer(ServiceFactory fact) {    Service s = fact.getService();    s.method1();    s.method2();  }  public static void main(String[] args) {    serviceConsumer(Implementation1.factory);    // Implementations are completely interchangeable:    serviceConsumer(Implementation2.factory);  }} /* Output:Implementation1 method1Implementation1 method2Implementation2 method1Implementation2 method2*///:~
确实是方便多了!

嵌套类,static,那在内存中就应该在特别的地方吧,在哪里呢?不需要外部类对象就可以建立内部类对象,这是要注意的一点。还有就是接口中也可以有内部类,可以是实现接口的类,这一点是不是有点奇怪,但是就是这样,一般是将一个接口实现时都需要的部分放在接口的内部类里面。还有就是想要测试时,用内部类,在产品发布时,又可以将内部类的class删除就好了。内部类有其外部类的引用,当然可以访问外部类的方法好理解。


为什么要内部类?这才是关键问题!!如果要是仅仅为了实现好多的接口,似乎不用内部类也可以,但是如果希望一个接口被多次不同地实现呢?用内部类可以办到!如果想要继承好多的非接口的类呢?不能多重继承那就用内部类来解决这个问题吧,可以的,没有问题。还有就是如果我继承了一个类和实现了一个接口,恰好其中有相同名称的方法,怎么办?当然,我指的是这两个方法应该是毫不相干的不同的方法,怎么办?那就用一个类继承一个类,在类中定义内部类实现接口,这样他们就有了不同的命名空间,则不会出现上面的问题了。闭包的功能(我没有十分明白什么是闭包),不过看这个程序还是让人心情挺舒畅的:

package innerclasses;//thinking in java P206//: innerclasses/Callbacks.java// Using inner classes for callbacksimport static net.mindview.util.Print.*;interface Incrementable {  void increment();}// Very simple to just implement the interface:class Callee1 implements Incrementable {  private int i = 0;  public void increment() {    i++;    print(i);  }}class MyIncrement {  public void increment() { print("Other operation"); }  static void f(MyIncrement mi) { mi.increment(); }}// If your class must implement increment() in// some other way, you must use an inner class:class Callee2 extends MyIncrement {  private int i = 0;  public void increment() {    super.increment();    i++;    print(i);  }  private class Closure implements Incrementable {    public void increment() {      // Specify outer-class method, otherwise      // you'd get an infinite recursion:      Callee2.this.increment();    }  }  Incrementable getCallbackReference() {    return new Closure();  }}class Caller {  private Incrementable callbackReference;  Caller(Incrementable cbh) { callbackReference = cbh; }  void go() { callbackReference.increment(); }}public class Callbacks {  public static void main(String[] args) {    Callee1 c1 = new Callee1();    Callee2 c2 = new Callee2();    MyIncrement.f(c2);    Caller caller1 = new Caller(c1);    Caller caller2 = new Caller(c2.getCallbackReference());    caller1.go();    caller1.go();    caller2.go();    caller2.go();  }} /* Output:Other operation112Other operation2Other operation3*///:~

确实这个程序还是蛮漂亮的!我还要好好体会一下!
继承内部类要使用enclosingClassReference.super()语法,覆盖或者说重写内部类时没有用的,都有自己的命名空间,仅仅是互补干扰的两个相同名称的内部类而已。

就这些吧,有点散乱,但就是这么多的知识点,我没有什么好的办法将它们联系起来,估计就是我下一遍读这本书的时候需要干的事情吧!

睡觉了,晚安!

0 0