This article mainly introduces the detailed explanation of Java callback mechanism and the specific details of the example code. Friends in need can refer to the following
Java callback mechanism
Summary:
I recently learned Java and came into contact with the callback mechanism (CallBack). It felt quite confusing when I first met it, and the relevant explanations I searched on the Internet were either given in one sentence, or they were relatively simple and seemed to provide a definition for CallBack. Of course, after I understood the callbacks, I went to read various explanations on the Internet, and there was really no problem. However, for me as a beginner, there is a step-by-step process missing. Here, I will describe my personal understanding of the callback mechanism in order from shallower to deeper. If there is anything wrong, please feel free to enlighten me!
Before you begin, imagine a scene: Kindergarten children have just learned addition within 10.
Chapter 1. The origin of the story
The kindergarten teacher wrote an equation "1 + 1 =" on the blackboard, and Xiao Ming filled in the blanks.
Since he has learned addition within 10, Xiao Ming can calculate this problem completely by himself. The code to simulate the process is as follows:
public class Student { private String name = null; public Student(String name) { // TODO Auto-generated constructor stub this.name = name; } public void setName(String name) { this.name = name; } private int calcADD(int a, int b) { return a + b; } public void fillBlank(int a, int b) { int result = calcADD(a, b); System.out.println(name + "心算:" + a + " + " + b + " = " + result); } }
When Xiao Ming was filling in the blanks (fillBalnk), he did a mental calculation (clacADD) and found that the result was 2, and wrote the result in the blank. The test code is as follows:
public class Test { public static void main(String[] args) { int a = 1; int b = 1; Student s = new Student("小明"); s.fillBlank(a, b); } }
The running results are as follows:
小明心算:1 + 1 = 2
This process is completely completed by the instance object of the Student class alone, and does not involve the callback mechanism.
Chapter 2. The kindergarten teacher’s search for trouble
During class, the kindergarten teacher had a sudden idea and wrote "168 + 291 =" on the blackboard for Xiao Ming to complete, and then Back to the office.
花开! Why do all the teachers have trouble with Xiao Ming? It's obviously over the top, okay! At this time, it was obvious that Xiao Ming could no longer rely on mental arithmetic as above. When he was confused, Xiao Hong, a classmate in the class, handed over a calculator that could only calculate addition (profiteer)! ! ! ! Xiao Ming happened to know how to use a calculator, so he used the calculator to calculate the results and fill in the blanks.
The code of the calculator is:
public class Calculator { public int add(int a, int b) { return a + b; } }
Modify the Student class and add a method to use the calculator:
public class Student { private String name = null; public Student(String name) { // TODO Auto-generated constructor stub this.name = name; } public void setName(String name) { this.name = name; } @SuppressWarnings("unused") private int calcADD(int a, int b) { return a + b; } private int useCalculator(int a, int b) { return new Calculator().add(a, b); } public void fillBlank(int a, int b) { int result = useCalculator(a, b); System.out.println(name + "使用计算器:" + a + " + " + b + " = " + result); } }
The test code is as follows:
public class Test { public static void main(String[] args) { int a = 168; int b = 291; Student s = new Student("小明"); s.fillBlank(a, b); } }
The running results are as follows:
小明使用计算器:168 + 291 = 459
The callback mechanism is still not involved in this process, but some of Xiao Ming's work has been transferred and is assisted by the calculator.
3. The kindergarten teacher came back
and found that Xiao Ming had completed the addition of three digits. The teacher thought that Xiao Ming was very smart and a malleable talent. So he wrote "26549 + 16487 = " on the blackboard and asked Xiao Ming to fill in the blanks before going to class, and then returned to the office.
Xiao Ming looked at the friends having fun outside the classroom and couldn't help feeling sad. If we don’t go out to play, this recess will be ruined! ! ! ! Looking at the calculator that Xiao Hong handed over again, Xiao Ming came up with a plan: let Xiao Hong do it for her.
Xiao Ming told Xiao Hong that the topic was "26549 + 16487 = ", then pointed out the specific location where the result should be filled in, and then went out to play happily.
Here, instead of implementing Xiaohong separately, we consider this calculator that can only perform addition and Xiaohong as a whole, a super calculator that can calculate results and fill in the blanks. The parameters that need to be passed to this super calculator are two addends and the positions to be filled in, and these contents need to be informed by Xiao Ming in advance, that is, Xiao Ming wants to expose part of his method to Xiao Hong. The simplest way is to Tell Xiaohong the quote and the two addends.
Therefore, the add method of the super calculator should contain two operands and a reference to Xiao Ming himself. The code is as follows:
public class SuperCalculator { public void add(int a, int b, Student xiaoming) { int result = a + b; xiaoming.fillBlank(a, b, result); } }
Xiao Ming’s Bian no longer needs mental arithmetic or a calculator, so he only needs a way to ask Xiaohong for help. The code is as follows:
public class Student { private String name = null; public Student(String name) { // TODO Auto-generated constructor stub this.name = name; } public void setName(String name) { this.name = name; } public void callHelp (int a, int b) { new SuperCalculator().add(a, b, this); } public void fillBlank(int a, int b, int result) { System.out.println(name + "求助小红计算:" + a + " + " + b + " = " + result); } }
The test code is as follows:
public class Test { public static void main(String[] args) { int a = 26549; int b = 16487; Student s = new Student("小明"); s.callHelp(a, b); } }
The running result is:
小明求助小红计算:26549 + 16487 = 43036
The execution process is: Xiao Ming calls the add method of Xiao Hong (new SuperCalculator()) through his own callHelp method, and passes in his own reference (this) as a parameter when calling. After Xiaohong used the calculator to get the result, she called back Xiaoming's fillBlank method and filled the result in the blank space on the blackboard.
Light, light, light! At this point, the callback function officially appears. Xiao Ming's fillBlank method is what we often call the callback function.
Through this method, it can be clearly seen that for the task of completing the teacher's fill-in-the-blank question, Xiao Ming no longer needs to wait until the addition is completed and the result is filled in on the blackboard before he can have fun with his friends. Well, filling in the blanks was done by the super calculator Xiaohong. The advantages of the pullback have already begun to show.
Chapter 4. Mother-in-law at the door
幼稚园的门口有一个头发花白的老婆婆,每天风雨无阻在那里摆着地摊卖一些快过期的垃圾食品。由于年纪大了,脑子有些糊涂,经常算不清楚自己挣了多少钱。有一天,她无意间听到了小明跟小伙伴们吹嘘自己如何在小红的帮助下与幼师斗智斗勇。于是,婆婆决定找到小红牌超级计算器来做自己的小帮手,并提供一包卫龙辣条作为报酬。小红经不住诱惑,答应了。
回看一下上一章的代码,我们发现小红牌超级计算器的add方法需要的参数是两个整型变量和一个Student对象,但是老婆婆她不是学生,是个小商贩啊,这里肯定要做修改。这种情况下,我们很自然的会想到继承和多态。如果让小明这个学生和老婆婆这个小商贩从一个父类进行继承,那么我们只需要给小红牌超级计算器传入一个父类的引用就可以啦。
不过,实际使用中,考虑到java的单继承,以及不希望把自身太多东西暴漏给别人,这里使用从接口继承的方式配合内部类来做。
换句话说,小红希望以后继续向班里的小朋友们提供计算服务,同时还能向老婆婆提供算账服务,甚至以后能够拓展其他人的业务,于是她向所有的顾客约定了一个办法,用于统一的处理,也就是自己需要的操作数和做完计算之后应该怎么做。这个统一的方法,小红做成了一个接口,提供给了大家,代码如下:
public interface doJob { public void fillBlank(int a, int b, int result); }
因为灵感来自帮小明填空,因此小红保留了初心,把所有业务都当做填空(fillBlank)来做。
同时,小红修改了自己的计算器,使其可以同时处理不同的实现了doJob接口的人,代码如下:
public class SuperCalculator { public void add(int a, int b, doJob customer) { int result = a + b; customer.fillBlank(a, b, result); } }
小明和老婆婆拿到这个接口之后,只要实现了这个接口,就相当于按照统一的模式告诉小红得到结果之后的处理办法,按照之前说的使用内部类来做,代码如下:
小明的:
public class Student { private String name = null; public Student(String name) { // TODO Auto-generated constructor stub this.name = name; } public void setName(String name) { this.name = name; } public class doHomeWork implements doJob { @Override public void fillBlank(int a, int b, int result) { // TODO Auto-generated method stub System.out.println(name + "求助小红计算:" + a + " + " + b + " = " + result); } } public void callHelp (int a, int b) { new SuperCalculator().add(a, b, new doHomeWork()); } }
老婆婆的:
public class Seller { private String name = null; public Seller(String name) { // TODO Auto-generated constructor stub this.name = name; } public void setName(String name) { this.name = name; } public class doHomeWork implements doJob { @Override public void fillBlank(int a, int b, int result) { // TODO Auto-generated method stub System.out.println(name + "求助小红算账:" + a + " + " + b + " = " + result + "元"); } } public void callHelp (int a, int b) { new SuperCalculator().add(a, b, new doHomeWork()); } }
测试程序如下:
public class Test { public static void main(String[] args) { int a = 56; int b = 31; int c = 26497; int d = 11256; Student s1 = new Student("小明"); Seller s2 = new Seller("老婆婆"); s1.callHelp(a, b); s2.callHelp(c, d); } }
运行结果如下:
小明求助小红计算:56 + 31 = 87 老婆婆求助小红算账:26497 + 11256 = 37753元
最后的话
可以很明显的看到,小红已经把这件事情当做一个事业来做了,看她给接口命的名字doJob就知道了。
有人也许会问,为什么老婆婆摆摊能挣那么多钱? 你的关注点有问题好吗!!这里聊的是回调机制啊!!
我只知道,后来小红的业务不断扩大,终于在幼稚园毕业之前,用挣到的钱买了人生的第一套房子。
The above is the detailed content of Detailed explanation of Java callback mechanism and specific details of example code. For more information, please follow other related articles on the PHP Chinese website!