Again encountered a callback function, this time to write down to share.
A callback function, or called a callback method in an object-oriented language, is simply a function that is called back at a certain time (the event occurs).
More detail: is a function A, as a parameter, passed in another function B, and then by B at some time call.
Here can be a question, since it is a function to call another function, you can call in the function body, why do you want to pass the function as a parameter to another function to be called? Besides, some languages (such as Java) do not support functions as arguments.
Yes, you can actually call another function in the body of the function, but there's a problem here, which is that the function you're calling is written dead, which means that function B can only call function A, so if in another scenario, There is a different implementation of a function C also need to be called at some point in B, then what to do.
Let's continue with the callback function, in C + +, where the callback function can be called by another function using the function pointer as a parameter; in C #, you can use a delegate, and if it is an event method, there is an event keyword; in Python and JavaScript, you can simply pass functions as objects. These languages are good for implementing callback functions (methods), but what about Java? First of all, Han, since learning C #, do not like Java, 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 the problem of this callback function, but also encountered in Java, I personally think that the language that appears in this blog, in addition to Java, for callbacks, can be easy, but also a good understanding of the implementation, but Java, I think it is not that, otherwise I will not write this blog.
All right, go on, about the implementation of callback methods in Java. The point of this blog is to say java. In Java, the callback method is implemented using a borrowed interface, and I found a sentence on the Internet:
"A reference to an object created by a class that implements an interface is assigned to an interface variable declared by that interface, and then the interface variable can invoke the method of the implemented interface."
It's a very round, simple explanation:
There is an interface in which there is a method (this method is the method to be invoked):
Interface Callbackinterface {
void Callbackmethod ();
}
We know that interface objects cannot be used directly, because the methods inside are not implemented. So find a class to implement this interface.
So now add a class to implement this interface:
Interface Callbackinterface {
void Callbackmethod ();
}
Class Callbackclass implements callbackinterface{
@Override public
void Callbackmethod () {
System.out.println ("Hello");
}
OK, the final 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 class shell outside):
public class Callbacktest {
interface callbackinterface {
void Callbackmethod ();
}
Class Callbackclass implements Callbackinterface {
@Override public
void Callbackmethod () {
System.out.println ("Hello");
}
public void Showcallback () {
Callbackinterface itfs = new Callbackclass ();
Itfs.callbackmethod ();
}
Now you can call to try it out:
public class Test {public
static void Main (string[] args) {
new Callbacktest (). Showcallback ();
}
}
If it's not an accident, it will output Hello, I'm here anyway.
The example was finished, 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 that it is best not to directly write to the callback method directly in the call method, and because Java can not pass the method as a parameter, So we have to put this callback method in the interface (why not the class?) instead of the abstract class? You can find out the similarities and differences between abstract classes and interfaces to solve this problem yourself. If there is an interface, it will be implemented by the class, and the object of the interface can call that method as long as it is given an object to the object of the implementation class. Understanding here, there is a point, is polymorphism, where the polymorphic knowledge is that interface objects can be successfully quilt class assignment, and call the subclass of the Rewrite method (class also has a similar concept).
To say a little bit more, any class that implements the Callbackinterface interface can be placed behind new (that is, assignment) as follows:
public class Callbacktest {interface Callbackinterface {void Callbackmethod (); Class Callbackclass implements Callbackinterface {@Override public void Callbackmethod () {System.ou
T.println ("Hello");
} class Controller {private Callbackinterface CBITF;
This boolean is only intended to simulate an event, with no practical value public boolean somethinghappend; It is true that the callbackclass can be directly used as a parameter, and the definition of the interface is omitted/But it is like a callback function written directly in the calling function. If you don't understand it, you'll understand. "Convention" and "caller no matter how the callback function is implemented" PU
Blic 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 ();
In fact, the above can also be written in one line//new Controller (New Callbackclass ()). DoSomething (); }
}
Last but not least, this kind of application is often encountered in Android, which I encountered when I was learning Android.
The above is the individual to the callback function of the understanding and use of methods, I hope you can enjoy.