Explanation of JAVA callback mechanism and java callback mechanism
I. Overview of callback mechanism
The callback mechanism has always been encountered in JAVA code, but I didn't understand its principle before, and it almost went around. As the saying goes, what you don't want to do is a breakthrough. This article was born. It is a new form of the new year and a new breakthrough! What is the callback mechanism? In fact, the callback mechanism has been around us all the time, but we are not aware of it when we get used to it. It is very easy to understand its principle. For example, set the following scenario: Instructor, student A with poor scores, and student B with good scores. The detailed steps are as follows: (1) the instructor asked student A to calculate a question A + B =? (2) student A suffers from the skill cd (The Courtier cannot do it) and does not calculate it. Ask student B for help (3) student B calculates the final answer based on the values of A and B that student a tells him, and then sends it to student A (4) Student A to learn the calculated answer, happily tell instructors 2. Implement code parsing (1) first define a unified interface
public interface CallBack {public void solve(int result);}
(2) Implementation of Class A. Note that during StudentA initialization, StudentB objects need to be passed in to facilitate the specified objects when requesting help.
public class StudentA implements CallBack{private StudentB mStuB;public StudentA(StudentB mStuB){this.mStuB = mStuB;}public void askQuestion(final int a,final int b){new Thread(new Runnable(){@Overridepublic void run() {// TODO Auto-generated method stubmStuB.executeMessage(StudentA.this, a, b);}}).start();}@Overridepublic void solve(int result) {// TODO Auto-generated method stubSystem.out.println(result);}}
(3) Implementation of Class B
Public class StudentB {public void executeMessage (CallBack callBack, int a, int B) {System. out. println ("A's question:" + a + "+" + B + "=? "); Int result = a + B; callBack. solve (result );}}
(4) value Initialization
public class CallBackTest {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubint a = 1;int b = 1;StudentB mStuB = new StudentB();StudentA mStuA = new StudentA(mStuB);mStuA.askQuestion(a,b);}}
Iii. Summary:
- Class A implements the CallBack callback --Background 1
Public Class StudentA implements CallBack
- Class A contains A reference of class B B --Background 2
When Student A initializes, it is passed into the object mStuB of Student B to facilitate the execution of the askQuestion method, there is A pointing mStuB object
- Class B has a callback method f (CallBack callback )--Background 3
After Student B executes the request, it needs to call back to notify StudentA. Therefore, it needs to be passed through the callback variable and uses the parent class reference to point to the subclass object.
- Object A of a calls B's method f (CallBack callback) -- class A calls A method C of Class B
MStuA. askQuestion (a, B)-> mStuB.exe cuteMessage (StudentA. this, a, B)
- Then, B can call the method A in the f (CallBack callback) Method -- class B calls A method D in Class.
ExecuteMessage ()-> callBack. solve (result) (parent class reference pointing to subclass object) 4. related recommendation http://blog.csdn.net/xiaanming/article/details/8703708/http://blog.csdn.net/pi9nc/article/details/23169357