Call back. The first time I saw the idea of Java programming, the Observer pattern also applied to the idea of a callback function. However, there has been no attention, at the disadvantage of the final interview, more and more understand why so many people say the idea of Java programming, this book should be over and over again.
First search on the Web very many articles describe what a callback function is. Look at the foggy. Later, I finally understood the step-by-step decomposition. Records such as the following.
Extract the definition from Wikipedia: (read this article to understand the definition)
in the Computer Programming the. A callback function , or callback, refers to a
Title=%e5%87%bd%e6%95%b0%e5%8f%82%e6%95%b0&action=edit&redlink=1 "> function Parameters are passed to other code. A reference to a piece of code that can be run . This design agrees with the underlying code call subroutine defined at the top level .
In Java. We use interfaces to implement callbacks.
The explanations in this article are very well defined: http://blog.csdn.net/sweetwxh/article/details/2067139
The so-called callback is that the program ape a wrote a program (program a), which reserved a callback function interface, and encapsulated the program.
Program Ape B to let a call itself a method in program B. He then callbacks his method in B through the interface in a.
The following is a sample example.
1. First define a class caller, according to the above definition is program ape a written by program A. This class holds an interface reference.
public class Caller {private Mycallinterface callinterface;public Caller () {}public void Setcallfunc (mycallinterface Callinterface) {this.callinterface = Callinterface;} public void Call () {callinterface.printname ();}}
2. Of course, the definition of the interface, in order to facilitate the program ape B according to my definition of code to implement the interface.
Public interface Mycallinterface {public void printname ();}
3. The third is to define program B written by Ape B
Public class Client implements Mycallinterface {@Overridepublic void Printname () {System.out.println ("This is the client P Rintname method ");}}
4. Tests such as the following
public class Test {public static void main (string[] args) {Caller Caller = new Caller (); Caller.setcallfunc (new Client ()); c Aller.call ();}}
See here should be clear what is a callback. Some of the articles are very good. But it didn't look clear at the beginning. is because the class of the 3rd step is omitted, written directly to the anonymous class.
5. Use anonymous classes directly in the test method. The 3rd step is omitted.
public class Test {public static void main (string[] args) {Caller Caller = new Caller ();//caller.setcallfunc (new Client ()) ; Caller.setcallfunc (New Mycallinterface () {public void Printname () {System.out.println ("This is the client Printname Method ");}); Caller.call ();}}
After reading the above article,. Look directly at the following article: http://kidult.iteye.com/blog/148982
These are the callback functions that are understood.
Learn more about callback functions Java