Comments interface callback and comments callback
Interface callback is a relatively abstract but very important knowledge. Most beginners will feel unable to grasp the essentials when they are new to it, but when we actually grasp it, we will love it. Let's get started.
The habit of knowing new things is to start with the name of things. How much information can we get from the interface callback? What is an interface first? I believe all those who have a certain programming Foundation know that it is a standard, but it has no effect before it is implemented. Just like a recruitment advertisement, the conditions listed on the advertisement are broad. For example, I want to recruit a programmer familiar with Java. This is a specification. If you want to apply for a job, you must satisfy the Java-related requirements. However, the situations may vary widely. It may be male, female, or master, this condition may also be met. However, as long as this condition is met, you can apply for an application as a candidate. Here, the recruitment condition is the interface, and the applicant is the implementation. How can I understand callback? Callback -- return to call, where? Return to the definition and implementation. Does this indirectly State a fact, that is, the place where I implemented the interface is different from the place where I called the interface. The question is clear.Interface callback means that I have a function, but I don't have this function, but there is an entity that has this function, and I can implement it through its function.
Is it still dizzy? Yes, let's proceed. For example, the document editor usually provides the print function. When I call the print function, if I do not provide it with a printer that works properly, it cannot work. However, after we connect the printer, We can print the content at the exit of the printer. But if I didn't call this printing function in the document Editor, isn't there any printing results? Is it amazing.This is the charm of interface callback. I have functions, but it is not me who really do things, but you cannot do things without me.
So what is the significance of this? For another example, what should I do if I need to paint the house but I cannot paint it myself? You must find someone to paint the house, instead of asking the painter if I need to paint the house. Because the paint time is uncertain, it is impossible to paint at any time. After judging based on various situations, at the right time, we will ask the painter to complete our paint task. When we do not need to paint, the painter may serve others, which improves the working efficiency of the painter and will not affect my paint task.Therefore, the meaning of the interface callback is very clear. I will direct what I want to do, but I will not do anything. We will switch from the performer's identity to the conductor, so that we can easily organize our functions, it will not waste the valuable resources of the hacker.
After talking about it for a long time, how should we implement interface callback? For example, I wrote a small test program to paint the house above.
1 package andy. example; 2 3 public interface Paint {4 // paint Task 5 void Paint (); 6}
Here, we define an interface with only one paint method.
1 package andy. example; 2 3 // Worker entity, with Paint function 4 public class Worker implements paint {5 6 @ Override 7 public void Paint () {8 System. out. println ("I'm a painter with strong paint skills! "); 9} 10 11}
Paint workers with paint functions.
1 package andy. example; 2 3 // The entity 4 public class Asker {5 6 // whether the House needs to be painted 7 private boolean isNeedPaint; 8 9 public void setIsNeedPaint (boolean isNeedPaint) {10 this. isNeedPaint = isNeedPaint; 11} 12 13/** 14 * command Paint 15*16 * @ param worker17 * painter 18 */19 public void doPaint (paint Paint) {20 if (isNeedPaint) {21 paint. paint (); 22} else {23 System. out. println ("the house does not need to be painted. Do you want to tease me! "); 24} 25} 26}
The person who may need paint.
1 package andy. example; 2 3 // Test class 4 public class Test {5 6 public static void main (String [] args) {7 // There are two requesters 8 Asker asker1 = new Asker (); 9 Asker asker2 = new Asker (); 10 11 // The second one really needs to paint 12 asker2.setIsNeedPaint (true); 13 14 // The same painter 15 Worker worker = new Worker (); 16 17 System. out. println ("the requester who does not need to paint the house, after calling the paint method"); 18 asker1.doPaint (worker); 19 System. out. println ("the requester who needs to paint the house, after calling the paint method"); 20 asker2.doPaint (worker); 21} 22 23}
Test class.
No doubt about the results.
Simple analysis: the person who needs to paint the house determines whether the House needs to be painted based on the situation. It is invalid to call the paint function when no paint is needed. For example, if result 1 is displayed, the person who actually needs to paint the house, call the paint method when appropriate to complete the painting.