Home » Java » The most simple example to understand java callbac...

The most simple example to understand java callback


Previously has been very confused callback mechanism, see online what classic example, let you thoroughly understand the callback and other articles, look after is still very confused. Later, listen to a teacher talked about, thoroughly understand.



A simple, tough example:



Public class EA {



/**

* defines an interface,

* @author dell

*

*/

interface mCallBack{

public, void, execute1 ();



public, void, execute2 ();

}



// construction method

public, EA () {

}

/**

* exposure method

* @param callBack

*/

public, void, doSomeThing (mCallBack, callBack) {

/**

* exposes these two methods to the EB class and lets him implement
himself
*/

callBack.execute1 ();

callBack.execute2 ();

}

}



Call EA

in the EB class


Public class EB {

/**

* @param args

*/

public, static, void, main (String[], args) {

EA, a=new, EA ();

a.doSomeThing (New mCallBack () {

The two methods, //, are implemented in EB, but
is executed in EA
public, void, execute2 () {

System.out.println ("execute2");

}



public, void, execute1 () {

System.out.println ("execute1");

}

});

}

}

Latest