The whole article will use examples to tell how to understand the example of notes can be divided into two examples of similar to understand a can be made by analogy, to understand the profound or to beat again /. /
Taking takeout as an example, customers order food on takeout software delicious and fun (≧▽≦ 6 motion)
public interface ICallBack{// The interface provided by merchant 0 (customers can eat their own dishes at this point)
// The callback function
void postExecute(a); // Order function provided by merchant 0 (for customers to use)
}
public interface ICallBack1{Companies / / 1
void postExecute(a);
}
public class EatWhat { // Ele. me software specifically provides callback scenarios for merchants and customers, which is actually provided by the system
// Used by the user
private ICallBack callBack; // Store 0
private ICallBack1 callBack1;// Store 1's store
public void setCallBack(ICallBack callBack){// Find the corresponding merchant through Ele. me
this.callBack=callBack; // By the customer
}
public void setCallBack1(ICallBack1 callBack1){/ / same as above
this.callBack1=callBack1;
}
public void eatSth(a){ // Ele. me gives customers' orders to merchants to implement
if(callBack! =null){
callBack.postExecute();
}
if(callBack1! =null){ callBack1.postExecute(); }}}public class Test{ // Specific callback scenario: customer :--> open are you hungry --> open corresponding merchant --> fill in the order
// --> execute the order by sending the customer's callback function to the merchant via ele. me
public static void main(String... args){
// Now there is a customer who can not go out to eat at home, so the customer (a new one) opens the door
EatWhat eatWhat=new EatWhat();
eatWhat.setCallBack1(new ICallBack1() {// The customer likes store 1 and takes the order at the same time,
//
@Override
public void postExecute(a) {
System.out.println("I've already decided I'm going to eat your chili stir-fry.");// Fill in the order information}});// To confirm the order, the call to eatSth() will invoke the customer's own implementation of the callback method --> to eat chili fried meateatWhat.eatSth(); }}Copy the code
Business specific logic
- : Merchants expose the corresponding ordering function:
postExecute();
. - : Ele. me software provides merchant’s member variables
Customer specific logic
-
Open the software :new EatWhat();
-
Click on the merchant and write your own order information :setCallBack(new ICallBack1(){.. });
-
Customers submit orders through Ele. me eatWhat.eatsth ();
Look at this and think about it further:
SetCallBack (new CallBack(){}); CallBack(new CallBack(){});
Two, now look at the followingandroid
Inside the callback, will be very familiar with it, look at the same code
public interface OnClickListener {
void onClick(View v);
}
public void setOnClickListener( OnClickListener l) {
if(! isClickable()) { setClickable(true);
}
getListenerInfo().mOnClickListener = l; // Add your listener to the listener array
}
public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() { // Assign to the pointer
@Override
public void onClick(View v) {
System.out.println("I'll have the chili fried meat."); }}); }}Copy the code
Comparing the two codes, the second one is missing the implementation because when we click the button, the system has already implemented the click event for us, otherwise it would not be called a listener.
The follow-up will be improved if there is insufficient more inclusive!