I encountered the callback function again, this time I plan to write down and share it.
The so -called callback function, or callback method in the object -oriented language, simply speaking, it is a function that is called at a certain time (event occurs).
For more details: It is a function A, as a parameter, which is passed into another function B, and then B is called by B at some time.
There can be doubt here. Since one function calls another function, you can call it in the function body. Why should the function pass the function as a parameter to another function? Besides Use the function as a parameter.
Yes, you can indeed call another function in the function body. It seems that there is no difference in function, but there is a problem here, that is, the function you want to call is written to death, which means that this function B can only call the function B can only call the function. A, so that if there is a function C differently implemented from A in another scenario, it needs to be called at a certain moment of B. What should I do?
Let's continue to talk about the callback function. In C/C ++, the callback function can be called by the function pointer as a parameter by another function; in C#, you can use the commission. If it is an event method, there are Event keywords; in Python and JavaScript You can directly pass the function as an object. These languages are well achieved to realize the callback function (method), but what about Java? Use Java, but the reality is not so ideal. I have to do Android now, so I still can't let go of Java, and today I encountered the problem of this callback function, which I encountered from Java. I personally feel that in this blog Language, except for Java, can be easy and understanding the realization of the callback, but Java, I don't think so, otherwise I won't write this blog.
Well, continue to say, the realization of the recovery method in Java. The focus of this blog is Java. In Java, the callback method is implemented by borrowing interface. I found a sentence on the Internet:
"The reference to the object created by the class that implements a certain interface is assigned to the interface variable of the interface statement, so the interface variable can call the implementation method of the implementation of the interface."
Very round, brief explanation:
There is an interface, there is a method in the interface (this method is to call back):
interface callbackInterface {void callbackMethod ();} We know that interface objects cannot be used directly, because the methods inside are not realized. So find a class to implement this interface.
So add a class now to implement this interface:
interface callbackInterface {void callbackMethod ();} Class CallbackClass Implements CallbackInterface {@Override Public VoidMetho d () {system.out.println ("hello");}}Well, the last step: assign an object that implements the class that implements the interface to the interface variable of the statement (I wrote it into a method, and then add a type of shell outside):
Public class callbackTest {interface callbackInterface {void callbackMethod ();} Class CallbackClass Implements CallbackInterface { E public void callbackMethod () {system.out.println ("Hello");}} Public void Showcallback () {CallbackInterface itfs = new callbackClass (); ITFS.CALLBACKMETHOD ();}}Now you can call and try:
Public class test {public static void main (string [] art) {new callbackTest (). Showcallback ();}}If no accident, I will successfully output Hello, anyway, I am here.
After reading the example, what do I do? Let's put it in detail, we have a method to be called in a certain method (this method is the callback method). We also said before Write directly in the call method in the callback method, and because Java cannot transmit the method as a parameter, we have to put this callback method in the interface (why not a class? Not an abstract class? Instead of interface? You can find the similarities and differences between the abstract category and the interface by yourself and solve this problem by yourself). If there is an interface, it will be implemented by class. Then, as long as the object of the interface is given to the object of the interface, the object of this interface can be called that method. If you understand this, there is a focus, which is polymorphism. The polymorphism you use here is that the object of the interface can be assigned smoothly, and the rewriting method of the subclass (the class also has similar concepts).
To put it one more, any class that implements the CallbackInterface interface here can be placed behind new (that is, assignment): the following (that is, the assignment):
Public class callbackTest {interface callbackInterface {void callbackMethod ();} Class CallbackClass Implements CallbackInterface { E public void callbackMethod () {system.out.println ("Hello");}} Class Controller {Private CallbackInterface CBITF; // This boolean Just to simulate incidents, there is no practical value, Public Boolean Somethinghaputhapinghaping; // It is indeed a parameter for callbackClass, and saving the definition of the interface // The same // If you do n’t understand, you can understand it. "The call" and the "call the callback function is implemented." dosomething () { if (somethinghaping) {cbitf.callbackMethod ();}}} public void showback () {callbackClass CBC = new callbackClass (); Controller CTR lr = new Controller (CBC); ctrlr.dosomething (); // In fact Write in one line // new controller (new callbackclass ()). Dosomething ();}}In the end, this is often encountered in Android. I encountered it when I was studying Android.
The above is the understanding and use of individuals' recovery functions. I hope everyone can like it.