Again, the callback function, this time to write down to share. The level is limited, if there are errors, please correct me. Reproduced please note the source.
The so-called callback function, or in the object-oriented language called the callback method, simply speaking, is going back at a certain time (event) is called function.
More detail: A function A, as a parameter, passed in another function B, and then called by B at a certain time.
Here can be questioned, since it is a function call another function, you can call in the function body, why also the function as a parameter to another function is called? Besides, some languages (such as Java) do not support functions as arguments.
Yes, it is true that another function can be called in the function body, functionally, but there is a problem, that is, the function you want to call is written dead, which means that function B can only call function A, so that if in another scenario, There is a function C that differs from a, which needs to be called at some point in B.
The following continues to say the callback function, the callback function can use a function pointer as a parameter in C + + to be called by another function, in C #, you can use the delegate, if it is an event method, there is the event keyword, in Python and JS, you can directly to the function as the object of the argument, These languages are well-implemented callback functions (methods), but what about Java? First of all, humorous, since learning C #, I think Java is really difficult to use, once intended to no longer use Java, but the reality is not so ideal, I now want to do Android, so still can not put down Java, and today encountered this callback function problem, is also encountered in Java, I personally think that the language in this blog, in addition to Java, for callbacks, can be both easy and good understanding of the implementation, but Java, I think it is not the case, or I will not write this blog.
all right, go ahead. The implementation of the callback method in Java. The point of this blog is java. In Java, the callback method is implemented using a borrowing interface, and I find a word on the Internet: "To assign a reference to an object created by a class that implements an interface to an interface variable declared by that interface, the interface variable can invoke the method of the implemented interface" . very round, simple explanation under:
There is an interface, there is a method in the interface (this method is the method to callback)
Interface Callbackinterface { void Callbackmethod (); }
We know that the interface object cannot be used directly, because none of the methods are implemented. So find a class to implement this interface.
So now add a class that implements this interface.
Interface Callbackinterface { void Callbackmethod (); } Class Callbackclass implements callbackinterface{ @Override public void Callbackmethod () { System.out.println ("Hello"); } }
OK, the last step: Assign the object of the class that implements the interface to the declared interface variable (I write it in a method, and then add a shell to the outside):
public class Callbacktest {interface Callbackinterface {void Callbackmethod ();} Class Callbackclass implements Callbackinterface {@Overridepublic void Callbackmethod () {System.out.println ("Hello");}} public void Showcallback () {callbackinterface itfs = new Callbackclass (); Itfs.callbackmethod ();}}
you can now call to try it out:
public class Test {public static void main (string[] args) {new Callbacktest (). Showcallback ();}}
no accident, it will be successful output Hello, anyway my side is yes.
example, so what did I do? In more detail, we have a method to be called in a certain method (this method is the callback method), we also said before, it is best not to directly write the callback method directly in the calling method, And because there's no way to pass the method as a parameter in Java, we have to put this callback method in the interface (why not a class?) instead of an abstract class? You can find out the similarities and differences between abstract classes and interfaces yourself and solve this problem yourself. If there is an interface, it will be implemented by the class, and then, An object of this interface can invoke that method as long as it is given an object of the implementation class to the object of the interface. Understanding here, there is a point, is polymorphic , The use of the polymorphic knowledge is, The object of the interface can be successfully assigned a value, and the overridden method of the subclass is called (the class also has a similar concept).
say a little more, here any realization Callbackinterface Interface classes can be placed behind new (that is, assignment) as follows:
Callbackinterface itfs = new Callbackclass ();
so the code is abstracted, isn't it, anyway, it's just a realization. Callbackinterface Interface object, I don't care what kind of object it is, how it is implemented, and what it does in the implementation. All these don't need to be known by the caller here. I suddenly think of a C # book to say: The interface is a contract. Indeed, the situation here is that all classes adhering to this Convention can be assigned values, is called. In Java, this Convention can only be an interface, and C #, can be an interface, or it can be a delegate, in C + +, is a function pointer, in Python and JS, is an object.
Well, that's about it, and finally a code, the above revision, the above example is just to clear the red words, the following example should be used to the situation, that is, in a method of another class to invoke a callback method when an event occurs. (In fact, most of the callback is used in the event processing, in the Android code, often used to on*****. Finally, look at the following code, and C # The event much like Ah, but C # code is much simpler than this)
public class Callbacktest {interface Callbackinterface {void Callbackmethod ();} Class Callbackclass implements Callbackinterface {@Overridepublic void Callbackmethod () {System.out.println ("Hello");}} Class Controller {Private Callbackinterface cbitf;//This boolean is just to simulate an event with no practical value public Boolean somethinghappend;// It is true that the callbackclass can be directly used to make parameters, and the definition of the interface is omitted//But this is done as if the callback function is written directly in the calling function//does not understand the "contract" and "caller no matter how the callback function is implemented" bar public Controller (Callbackinterface itfs) {somethinghappend = TRUE;THIS.CBITF = Itfs;} public void DoSomething () {if (somethinghappend) {Cbitf.callbackmethod ();}}} public void Showcallback () {callbackclass CBC = new Callbackclass (); Controller CTRLR = new Controller (CBC), ctrlr.dosomething ();//The above can also be written in one line//new Controller (new Callbackclass ()). DoSomething ();}}
Callback function, which is the function to call back again