Java内部类之闭包(closure)与回调(callback)

闭包(closure)是一个可调用的对象,它记录了一些信息,这些信息来自于创建它的作用域。通过这个定义,可以看出内部类是面向对象的闭包,因为它不仅包含外围类对象(创建内部类的作用域)的信息,还自动拥有一个指向此外围类对象的引用,在此作用城内,内部类有权操作所有的成员,包括private成员。

 

   Java最引人争议的问题之一就是,人们认为Java应该包含某种类似指针的机制,以允许回调(callback)。通过回调,对象能够携带一些信息,这些信息允许它在稍后的某个时刻调用初始的对象。

 

   稍后将会看到这是一个非常有用的概念。如果回调是通过指针实现的,那么就只能寄希望于程序员不会误用该指针。然而,您应该已经了解到,Java更小心仔细,所以没有在语言中包括指针。


   通过内部类提供闭包的功能是优良的解决方案,它比指针更灵活、更安全。见下例:

[java]  view plain  copy
 print ?
  1. // innerclasses/Callbacks.java  
  2.   // Using inner classes for callbacks  
  3.   package innerclasses  
  4.   import static net.mindview.util.Print.*;  
  5.   
  6.   interface Incrementable  
  7.      void increment();  
  8.    
  9.   
  10.   
  11.   
  12.   // Very simple to just implement the interface  
  13.   class Callee1 implements Incrementable  
  14.      private int 0 
  15.      public void increment()  
  16.         i++  
  17.         print(i);  
  18.       
  19.    
  20.   
  21.   
  22.   
  23.   class MyIncrement  
  24.      public void increment() {print("Other operation");}  
  25.      static void f(MyIncrement mi) {mi.increment();}  
  26.    
  27.   
  28.   
  29.   // If your class must implement increment() in  
  30.   // some other way, you must use an inner class:  
  31.   class Callee2 extends MyIncrement  
  32.      private int i=0 
  33.      public void increment()  
  34.         super.increment();  
  35.         i++;  
  36.         print(i);  
  37.       
  38.      private class Closure implements Incrementable  
  39.         public void increment()  
  40.            // Specify outer-class method, otherwise  
  41.            // you'd get an infinite recursion  
  42.            Callee2.this.increment();  
  43.          
  44.       
  45.      Incrementable getCallbackReference()  
  46.         return new Closure();  
  47.       
  48.    
  49.   
  50.   
  51.   
  52.   class Caller  
  53.      private Incrementable callbackReference;  
  54.      Caller(Incrementable cbh) {callbackReference cbh;}  
  55.      void go() {callbackReference.increment();}  
  56.    
  57.   
  58.   
  59.   
  60.   public class Callbacks  
  61.      public static void main(String[] args)  
  62.         Callee1 c1 new Calleel();  
  63.         Callee2 c2 new Callee2();  
  64.         MyIncrement.f(c2);  
  65.   
  66.         Caller caller1 new Caller(c1);  
  67.         Caller caller2 new Caller(c2.getCallbackReference());  
  68.         caller1.go();  
  69.         caller1.go();  
  70.         caller2.go();  
  71.         caller2.go();  
  72.       
  73.    


输出:

 

   Other operation
   1
   1
   2
   Other operation
   2
   Other operation
   3

 

   这个例子进一步展示了外围类实现一个接口与内部类实现此接口之间的区别。就代码而言,Callee1是简单的解决方式。Callee2继承自MyIncrement,后者已经有了一个不同的increment()方法,并且与Incrementable接口期望的increment()方法完全不相关。

 

   所以如果Callee2继承了MyIncrement,就不能为了Incrementable的用途而覆盖increment()方法,于是只能使用内部类独立地实现Incrementable。还要注意,当创建了一个内部类时,并没有在外围类的接口中添加东西,也没有修改外围类的接口。

 

   注意,在Callee2中除了getCallbackReference()以外,其他成员都是private的。要想建立与外部世界的任何连接,interface Incrementable都是必需的。在这里可以看到,interface是如何允许接口与接口的实现完全独立的。

 

   内部类Cl

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值