I. Overview of CALLBACK mechanisms
Callback mechanism has been encountered in Java code, but did not understand its principle, almost all go around. As the saying goes, do not want to do things called breakthroughs, so the birth of the article, is a new year's atmosphere, a breakthrough! What is the callback mechanism? In fact, the callback mechanism has been around us, but we are accustomed to the existence of undetected, understand its principle is very simple. First of all, set the scene as follows, the teacher, poor grades students A, score good student B, the detailed steps are as follows: (1) The teacher let student a calculate a topic a+b=? (2) Student a suffers from the skill CD (the concubine cannot do), does not calculate, asks student B to help complete (3) Student B calculates the final answer according to the value of a and B that student a tells him, and then passes it to student a (4) student A learns the answer to the calculation, happily tells the teacher second, realizes code parsing (1 The interface
Public interface CallBack {public void solve (int result);
(2) The implementation of Class A is especially important to note that when Studenta is initialized, it is necessary to pass in the Studentb object, which makes it easier to have the specified object 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-genera Ted Method Stubmstub.executemessage (Studenta.this, A, b);}). Start ();} @Overridepublic void Solve (int result) {//TODO auto-generated method StubSystem.out.println (Result);}}
(3) Class B implementation
public class Studentb {public void ExecuteMessage (CallBack callback,int a,int b) {System.out.println ("a" problem is: "+a +" + "+ B +" =?"); int result = a + b;callback.solve (result);}}
(4) Initialization of the value
public class Callbacktest {/** * @param args */public static void main (string[] args) {//TODO auto-generated method Stubi NT A = 1;int B = 1; STUDENTB mstub = new Studentb (); Studenta Mstua = new Studenta (mstub); Mstua.askquestion (A, b);}}
Third, the summary of the reference on-line classic interpretation is as follows:
- Class a implements interface callback callback-- background 1
Public Class Studenta implements CallBack
- Class A contains a reference to Class B b-- background 2
When Student A is initialized, the object passed into Student B is mstub to facilitate the Mstub object that is directed when the Askquestion method is executed.
- Class B has a method with a parameter of CallBack F (CallBack CallBack)-- background 3
Student B requires a callback to inform Studenta after executing the request, so it needs to pass through the callback variable, using the parent class reference to point to the subclass object
- Object A of a calls B method F (CallBack CallBack)--a class calls a method of Class B
mstua.askquestion (b), Mstub.executemessage (Studenta.this, A, b)
- Then B can call a method in the F (CallBack CallBack) method--b class calls a method of Class D
executemessage (), Callback.solve (Result) (parent class reference to child class object)Iv. related recommendationshttp://blog.csdn.net/xiaanming/article/details/8703708/http://blog.csdn.net/pi9nc/article/details/23169357
Java Callback mechanism parsing