This article is mainly about Java serialization and static, multithreading concurrency processing, more Java technical knowledge, please visit the Crazy Software Education website.
Now there are two classes, one is a, one is B, the callback is a to call a method in B, and then B callback a method in a, this method is the callback method, the callback mechanism is inseparable from the interface. That is, Class A implements an interface and implements the interface method, and Class B gets a reference to the callback function, and then calls the swap function. It seems to be more around, give a popular example.
Now there is a teacher (Class A) and a student (class B), the teacher told the student said, put your homework to the office, the students said my homework has not finished, and so on, and then handed over, the teacher said well, so told the students where his office, and then the teachers and students to go to busy their own things. (This example also has async, will not be involved in the code to be asynchronous), after the meeting, the students handed over to the teacher designated office.
Here's a look at the code:
Callback interface equivalent to the teacher's office address
Public interface CallBack {
public void callback (String homework);
}
Teacher appears as an upper-level application
public class Teacher implements callback{
@Override
public void callback (String homework) {
System.out.println ("Teacher, this is my homework ..." +homework);
}
}
Students appear as lower-level identities
public class Student {
Equivalent to the teacher's office
Private CallBack CallBack = null;
public void Setcallback (CallBack CallBack) {
This.callback = CallBack;
}
public void Dohomework () {
System.out.println ("The Writing industry ... (half an hour later) the homework was finished ");
Tell the teacher that the homework has been finished and has been handed over to the office
Callback.callback ("A math paper");
}
}
public class Test {
public static void Main (string[] args) {
Teacher t = new Teacher ();
Student s = new Student ();
Incoming callback object teacher
S.setcallback (t);
S.dohomework ();
}
}
The above is an example of a callback. Callback is a callback interface, is the office address, the teacher is in the upper level, its students do not know and do not understand, so the teacher wants to let the students will work to the office, also must tell the student office address, so the teacher this class must go to implement this callback interface. In this class of teachers, you can also call a method of the student class, of course, in this case, not to call the Student class method, but only to implement the callback function, tell the students his homework received. In the student class, we first get a reference to the callback interface, which is the address of the office, and then the student class calls the callback function and puts the job into the office.
Crazy software Java training keep up with the latest Java Power Flow technology, the first in the country to explain the new Java 8 features, according to the needs of enterprise work in the development of technology, so that students learn the curriculum is the most needed practical technology, but also to ensure that students can learn these skills point, learn, and crazy software education courses in real-time updates, With the rapid development of information technology, students can be guaranteed to learn to update the more mainstream technology. Teachers are strong, the teacher has more than 8 years of project experience, Crazy software Java course is the combination of theory and practice, so as to ensure that the students have a good grasp of theoretical knowledge at the same time, can also be based on theoretical guidance to do practical work. In order to cultivate students ' practical ability, many practical courses have been set up, in which students can experience the real process of actual project, so as to master knowledge more deeply. For more information, please contact QQ:544627560 Hotline: 020-28309358 Consulting qq:707552864 can visit the Crazy Software education website to inquire.
Understanding of the Java callback interface