A callback function is a function written by others, but not called by others.
The message response function can be regarded as a callback function, because it allows the system to call the function at an appropriate time. However, the message response function is used to process messages. Therefore, a single message is used as a type. In essence, it is a callback function.
However, the callback function is not only a message response function. For example, in kernel programmingProgramSome callback functions are required. After a device reads and writes data, the system calls these callback functions to perform subsequent work.
Callback functions give programmers the ability to writeCodeIt can jump out of the normal program control flow and adapt to the specific running environment for execution at the right time.
Callback Function Summary
Definition given by others for callback:
The so-called callback means that object a calls a method B in another object B, and then B calls a function C in a at a time. For B, this C is called a callback function.
Callback is a two-way call mode, that is, the called party also calls the interface of the other party when the interface is called.
My understanding is similar to the combination of general classes. Object A calls a method B of object B. However, Method B is abstract and later bound.
Java callback code.
1. Define the callback interface.
Package Com. Smart; /** * Define the callback Interface */ Public Interface Callback { Void Execute ();} Package Com. Smart; /** * Define the callback Interface */ Public Interface Callback { Void Execute ();} Package Com. Smart; /** * Tool */ Public Class Tools { Public Void Test (callback ){ Long Begin = system. currenttimemillis (); // Test start time Callback.exe cute (); // /Perform callback operations Long End = system. currenttimemillis (); // Test End Time System. Out. println ("[use time]:" + (end-begin )); // Print usage time } Public Static Void Main (string [] ARGs) {tools =New Tools (); tools. Test ( New Callback (){ Public Void Execute (){ // A. Method (); the execution time of a method in Class. // B. Method (); the execution time of a method of test Class B. System. Out. Print ("Callback" );}});}} Package Com. Smart; /** * Tool */ Public Class Tools { Public Void Test (callback ){ Long Begin = system. currenttimemillis (); // Test start time Callback.exe cute (); // /Perform callback operations Long End = system. currenttimemillis (); // Test End Time System. Out. println ("[use time]:" + (end-begin )); // Print usage time } Public Static Void Main (string [] ARGs) {tools = New Tools (); tools. Test ( New Callback (){ Public Void Execute (){ // A. Method (); the execution time of a method in Class. // B. Method (); the execution time of a method of test Class B. System. Out. Print ("Callback" ) ;}}) ;}Advantages: I personally think the advantage is mainly to separate the variable and unchanged parts of the Code, which greatly improves the reusability and scalability of the program.