Detailed examples of Java callback mechanism

Detailed examples of Java callback mechanism

preface

Recently, I came into contact with the callback mechanism. When I first met him, I felt confused, and the relevant explanations found on the Internet were either passed in a word, or they were simple, like a definition of callback. Of course, after I understand the callback, I can go to see various explanations on the Internet. It's really no problem. However, for beginners, I lack a step-by-step process. Here, I will describe my personal understanding of the callback mechanism from shallow to deep. If there is anything wrong, please give me your advice!

Before starting, imagine a scene: the children in the kindergarten 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 the problem completely by himself. The code for simulating the process is as follows:

When filling in the blank, Xiao Ming did a direct mental calculation and got the result of 2. He wrote the result in the blank. The test code is as follows:

The operation results are as follows:

This process is completely completed by the instance object of student class, and does not involve callback mechanism.

Chapter 2 Kindergarten teacher's fault finding

During the recess, the kindergarten teacher had a whim and wrote "168 + 291 =" on the blackboard for Xiao Ming to finish, and then went back to the office.

Flower eraser! Why do all the teachers have trouble with Xiao Ming? It's beyond the outline, okay! At this time, Xiaoming obviously can't rely on mental calculation like the above. When he was confused, Xiaohong in the class handed over a calculator that can only calculate addition (profiteer)!!!! and Xiaoming just knew how to use the calculator, so he calculated the result through the calculator and filled in the blank.

The code of the calculator is:

Modify the student class and add a method to use the calculator:

The test code is as follows:

The operation results are as follows:

The callback mechanism has not been involved in this process, but part of Xiaoming's work has been transferred, which is assisted by the calculator.

3. The kindergarten teacher is back

It was found that Xiao Ming had completed the addition of three digits. The teacher thought Xiao Ming was very clever and a plastic talent. So he wrote "26549 + 16487 =" on the blackboard and asked Xiao Ming to fill in the blanks before class, and then went back to the office.

Xiao Ming looked at the happy little partner outside the classroom and couldn't help feeling sad. If you don't go out to play, this recess will be abandoned!!!! Looking at the calculator handed over by Xiaohong again, Xiaoming has a plan: let Xiaohong do it for him.

Xiao Ming tells Xiao Hong that the title is "26549 + 16487 =", then points out the specific location of the result, and then goes out to play happily.

Here, instead of realizing Xiaohong alone, we take Xiaohong and the calculator that can only calculate addition as a whole, a super calculator that can calculate results and fill in the blanks. The parameters that this super calculator needs to pass are two addends and the position to fill in the blank, and these contents need to be informed by Xiao Ming in advance, that is, Xiao Ming wants to leak some of his methods to Xiao Hong. The simplest way is to tell Xiao Hong his reference and two addends.

Therefore, the add method of the supercomputer should contain two operands and Xiaoming's own reference. The code is as follows:

Xiao Ming doesn't need to do mental arithmetic or use a calculator now, so he only needs a method to ask Xiao Hong for help. The code is as follows:

The test code is as follows:

The operation result is:

The execution process is as follows: Xiaoming calls the add method of Xiaohong (New hypercalculator ()) through her callhelp method, and when calling, she passes in her own reference (this) as a parameter. After Xiaohong obtains the result by using the calculator, she calls back Xiaoming's fillblank method and fills the result in the space on the blackboard.

Light, light! Here, the callback function is officially launched. Xiao Ming's fillblank method is what we often call the callback function.

In this way, it is obvious that Xiao Ming doesn't need to wait until the addition is completed and the result is filled on the blackboard to have fun with his friends. The job of filling in the blank is done by super calculator Xiao Hong. The advantages of callback have begun to manifest.

Chapter 4 The mother-in-law at the door

There is an old woman with gray hair at the gate of the kindergarten. Rain or shine every day, there is a stall selling some junk food that is about to expire. Because of his age, he is a little confused and often can't figure out how much money he has earned. One day, she overheard Xiao Ming and his friends boasting about how to fight with the kindergarten teacher with the help of Xiao Hong. So her mother-in-law decided to find a small red card super calculator to be her little helper and provide a bag of Weilong spicy sticks as a reward. Xiao Hong couldn't resist the temptation and agreed.

Looking back at the code in the previous chapter, we found that the parameters required for the add method of the little red card supercomputer are two integer variables and a student object, but my wife is not a student and a peddler. It must be modified here. In this case, we naturally think of inheritance and polymorphism. If Xiao Ming, a student, and her mother-in-law, a peddler, inherit from a parent class, we only need to pass in a reference to the parent class to the little red card supercomputer.

However, in actual use, considering the single inheritance of Java and the fact that you don't want to expose too many things to others, we use the method of inheriting from the interface in conjunction with the internal class.

In other words, Xiao Hong hopes to continue to provide calculation services to the children in her class, and also provide accounting services to her mother-in-law, and even expand the business of others in the future. Therefore, she has agreed a method to all customers for unified processing, that is, the operands she needs and what she should do after calculation. Xiaohong has made an interface for this unified method and provided it to everyone. The code is as follows:

Because the inspiration comes from filling in the blanks for Xiao Ming, Xiao Hong retains her original intention and does all her business as a fill blank.

At the same time, Xiao Hong modified her calculator so that she can handle different people who have implemented the dojob interface at the same time. The code is as follows:

After Xiaoming and her mother-in-law get the interface, as long as they implement the interface, it is equivalent to telling Xiaohong the processing method after obtaining the result according to the unified mode. They use the internal class according to the previous saying. The code is as follows:

Xiao Ming's:

Old woman's:

The test procedure is as follows:

The operation results are as follows:

Last words

It is obvious that Xiaohong has done this as a career. Just look at the name dojob she gave the interface life.

Some people may ask, why can the old woman make so much money by setting up a stall? There's something wrong with your focus, okay!! What we are talking about here is the callback mechanism!!

I only know that later, Xiaohong's business continued to expand. Finally, before graduating from kindergarten, she bought her first house with the money she earned.

I hope you can thoroughly understand this part through this article. Thank you for reading. I hope it can help you. Thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>