java回调函数简介

案例一
  下面使用java回调函数来实现一个测试函数运行时间的工具类:如果我们要测试一个类的方法的执行时间,通常我们会这样做:
  1. public   class  TestObject {  
  2.     /**  
  3.      * 一个用来被测试的方法,进行了一个比较耗时的循环  
  4.      */   
  5.     public   static   void  testMethod(){  
  6.         for ( int  i= 0 ; i< 100000000 ; i++){  
  7.               
  8.         }  
  9.     }  
  10.     /**  
  11.      * 一个简单的测试方法执行时间的方法  
  12.      */   
  13.     public   void  testTime(){  
  14.         long  begin = System.currentTimeMillis(); //测试起始时间   
  15.         testMethod(); //测试方法   
  16.         long  end = System.currentTimeMillis(); //测试结束时间   
  17.         System.out.println("[use time]:"  + (end - begin)); //打印使用时间   
  18.     }  
  19.       
  20.     public   static   void  main(String[] args) {  
  21.         TestObject test=new  TestObject();  
  22.         test.testTime();  
  23.     }  
  24. }  


  大家看到了testTime()方法,就只有"//测试方法"是需要改变的,下面我们来做一个函数实现相同功能但更灵活:

  首先定一个回调接口:

  1. public   interface  CallBack {  
  2.     //执行回调操作的方法   
  3.     void  execute();  
  4. }  


  然后再写一个工具类: 

  1. public   class  Tools {  
  2.       
  3.     /**  
  4.      * 测试函数使用时间,通过定义CallBack接口的execute方法  
  5.      * @param callBack  
  6.      */   
  7.     public   void  testTime(CallBack callBack) {  
  8.         long  begin = System.currentTimeMillis(); //测试起始时间   
  9.         callBack.execute(); ///进行回调操作   
  10.         long  end = System.currentTimeMillis(); //测试结束时间   
  11.         System.out.println("[use time]:"  + (end - begin)); //打印使用时间   
  12.     }  
  13.       
  14.     public   static   void  main(String[] args) {  
  15.         Tools tool = new  Tools();  
  16.         tool.testTime(new  CallBack(){  
  17.             //定义execute方法   
  18.             public   void  execute(){  
  19.                 //这里可以加放一个或多个要测试运行时间的方法   
  20.                 TestObject.testMethod();  
  21.             }  
  22.         });  
  23.     }  
  24.       
  25. }  

  大家看到,testTime()传入定义callback接口的execute()方法就可以实现回调功能

 

案例二

 程序员A写了一段程序(程序a),其中预留有回调函数接口,并封装好了该程序。程序员B要让a调用自己的程序b中的一个方法,于是,他通过a中的接口回调自己b中的方法。目的达到。在C/C++中,要用回调函数,被掉函数需要告诉调用者自己的指针地址,但在JAVA中没有指针,怎么办?我们可以通过接口(interface)来实现定义回调函数。
     假设我是程序员A,以下是我的程序a:

 

  1. public class Caller  
  2. {  
  3.     public MyCallInterface mc;  
  4.   
  5.     public void setCallfuc(MyCallInterface mc)  
  6.     {  
  7.        this.mc= mc;  
  8.     }  
  9.   
  10.     public void call(){  
  11.        this.mc.method();  
  12.     }  
  13. }      

  

 

 

     我还需要定义一个接口,以便程序员B根据我的定义编写程序实现接口。

  1. public interface MyCallInterface  
  2. {  
  3.     public void method();  
  4.   
  5. }  


 

     于是,程序员B只需要实现这个接口就能达到回调的目的了:

    1. public class B implements MyCallInterface  
    2. {  
    3.     public void method()  
    4.     {  
    5.        System.out.println("回调");  
    6.     }  
    7.   
    8.     public static void main(String args[])  
    9.     {  
    10.        Caller call = new Caller();  
    11.        call.setCallfuc(new B());  
    12.        call.call();  
    13.     }  
    14. }  

PS: 欢迎关注公众号"Devin说",会不定期更新Java相关技术知识。 

posted on 2012-04-24 14:44  Devin Zhang  阅读(4293)  评论(0编辑  收藏  举报