Java callback mechanism in detail:
- Callback method:
- Programmer A wrote a program (a1), which reserved an interface for the callback function and encapsulated the interface. Programmer B wants A1 to call a method in his own program b1, so he calls back the method in his own B1 through the interface in a1. Achieve the goal
- WTF, what are you talking about??????
- Programmer A wrote a program (a1), which reserved an interface for the callback function and encapsulated the interface. Programmer B wants A1 to call a method in his own program b1, so he calls back the method in his own B1 through the interface in a1. Achieve the goal
-
Can't understand the concept? Not lively enough? Okay, let's break it up and break it up:
-
Cook a practical chestnut:
- Man has a pet dog. One day, man was stood up by the goddess. Man was very angry. He called his own method and beat his dog. His dog was beaten and angry, so he bit someone back and felt very painful. So he shouted.
-
In the chestnuts above, we can see the model of callback.
- First:
- When a man beats a dog, the dog bites him back, and he cries out when he feels pain. This is a callback.
- Doubt:
- How can you trigger a dog to bite, and people cry out for pain?
-
This requires knowledge of callbacks.
- First, let's draw a picture.
-
Code example:
-
First, you need a callback interface:
//Callback interface, which defines the behavior of a person after being bitten. The method in the interface describes the behavior of a person after being bitten. public interface CallBack { public void callBack(); }
-
Secondly, we need a dog:
public class Dog { //In fact, the so-called callback does not really allow Dog to inform people of the pain. This is illogical. //So it's supposed to be an object with a callback interface inside the Dog that notifies people of the pain. //This object is assigned through Dog, and its source is Person, because Person implements this callback interface. CallBack mCallBack; public void setCallBack(CallBack callBack) { mCallBack = callBack; } public void bitePerson() { System.out.println("The dog bit back"); mCallBack.callBack(); } }
-
There's still one more person left.
//By implementing the callback interface, one can define the behavior after being bitten. public class Person implements CallBack { //At the same time, we need to encapsulate a dog object inside people. Dog mDog; public Person(Dog dog) { mDog = dog; //Assignment of Callback Interface Objects in Dogs mDog.setCallBack(this); } //Ways to beat a dog public void hitDog(){ System.out.println("The man was unhappy and beat the dog."); mDog.bitePerson(); } //The way to define a person's behavior after being bitten is the callback method. @Override public void callBack() { System.out.println("It hurts to be bitten by a dog."); } }
-
Main method implements the whole callback process:
public class CallMain { public static void main(String[] args) { Person person = new Person(new Dog()); person.hitDog(); } }
-
- First, let's draw a picture.
- First:
-
With the chestnuts mentioned above, I think you are not so familiar with the callback. Next, we cook another chestnut and eat a little more.
- Simulate Android Onclick click events
-
Code example:
-
Simulated Activity:
public class Activity implements OnClickListener { Button mButton; /** * onCreate() method in Activeness simulating Android */ public Activity(Button button) { //Simulate findViewById() mButton = button; mButton.setOnClickListener(this); } @Override public void onClick() { System.out.println("The button was clicked."); } /** * Simulated Button Click */ public void push() { mButton.performClick(); } }
-
Simulated Button:
public class Button { OnClickListener mOnClickListener; public void setOnClickListener(OnClickListener onClickListener) { mOnClickListener = onClickListener; } public void performClick() { //This is callback. mOnClickListener.onClick(); } }
-
Callback of simulated click events
public interface OnClickListener { public void onClick(); }
-
main method for simulating click process
public class ClickMain { public static void main(String[] args) { Activity activity=new Activity(new Button()); activity.push(); } }
-
- Simulate Android Onclick click events
-