Case 1 The following uses the Java callback function to implement a tool class for testing the function running time: if we want to test the execution time of a class method, we usually do this:
- PublicClassTestobject {
- /**
- * A method used for testing carries out a time-consuming cycle.
- */
- PublicStaticVoidTestmethod (){
- For(IntI =0; I <100000000; I ++ ){
- }
- }
- /**
- * A Simple Method for Testing the execution time
- */
- PublicVoidTesttime (){
- LongBegin = system. currenttimemillis ();// Test start time
- Testmethod (); // Test Method
- LongEnd = system. currenttimemillis ();// Test End Time
- System. Out. println ("[Use time]:"+ (End-begin ));// Print the usage time
- }
- PublicStaticVoidMain (string [] ARGs ){
- Testobject test =NewTestobject ();
- Test. testtime ();
- }
- }
As you can see, only the "// test method" of the testtime () method needs to be changed. Let's create a function to implement the same function but be more flexible:
First, define a callback interface:
- PublicInterfaceCallback {
- // Method for executing the callback operation
- VoidExecute ();
- }
Then write a tool class:
- PublicClassTools {
- /**
- * Test the function usage time by defining the execute method of the callback Interface
- * @ Param callback
- */
- PublicVoidTesttime (callback ){
- LongBegin = system. currenttimemillis ();// Test start time
- Callback.exe cute (); /// Perform callback
- LongEnd = system. currenttimemillis ();// Test End Time
- System. Out. println ("[Use time]:"+ (End-begin ));// Print the usage time
- }
- PublicStaticVoidMain (string [] ARGs ){
- Tools tool =NewTools ();
- Tool. testtime (NewCallback (){
- // Define the execute Method
- PublicVoidExecute (){
- // Add one or more methods to test the running time.
- Testobject. testmethod ();
- }
- });
- }
- }
As you can see, the callback function can be implemented by passing testtime () to the execute () method that defines the callback interface.
Case 2
ProgramMember a writes a program (program A), which reserves the callback function interface and encapsulates the program. Programmer B wants a to call a method in program B, So he calls back the method in B through the interface in. .In C/C ++, a callback function is used. If a function is dropped, the caller must be notified of its own pointer address, but there is no pointer in Java. What should I do? We can use interfaces to define callback functions.
Suppose I am a programmer, and the following is my program:
[Java] View plaincopyprint?
- Public ClassCaller
- {
- PublicMycallinterface MC;
- Public VoidSetcallfuc (mycallinterface MC)
- {
- This. MC = MC;
- }
- Public VoidCall (){
- This. MC. Method ();
- }
- }
I also need to define an interface so that programmer B can write the program implementation interface according to my definition.
[Java] View plaincopyprint?
- Public InterfaceMycallinterface
- {
- Public VoidMethod ();
- }
As a result, programmer B only needs to implement this interface to achieve the purpose of callback:
[Java] View plaincopyprint?
- Public ClassBImplementsMycallinterface
- {
- Public VoidMethod ()
- {
- System. Out. println ("Callback");
- }
- Public Static VoidMain (string ARGs [])
- {
- Caller call =NewCaller ();
- Call. setcallfuc (NewB ());
- Call. Call ();
- }
- }