java-- callback function

 Callback significance is that by calling itself a function of the class defined in another object, to achieve a certain purpose (common in event registration, monitoring and thread Runnable in the run).

public  class the Task {
     Private the Calculator Calculator = new new the Calculator ();
     public  void the Add ( int A, int B) {
         // the this transfer current object, in response to calling the callback method of the current class in other class 
        calculator.add (a, b , the this ); 
    } 
    // callback response 
    public  void callResult ( int result) { 
        System.out.println ( "The result is:" + result); 
    } 
}

 

public class Calculator {
    public void add(int a,int b,Task task){
        int result=a+b;
        //调用回调方法
        task.callResult(result);
    }
}

 

public class Test {
    public static void main(String[] args) {
        Task task1=new Task();
        task1.add(2,3);
    }
}

 

Guess you like

Origin www.cnblogs.com/jvStarBlog/p/11028979.html