Interface callbacks are a relatively abstract but important knowledge, most beginners will feel that they can not grasp the essentials when they touch it, but when we actually grasp it, we will love it. Don't say much nonsense, let's get started.
I know new things in general have such a habit, is to start with the name of the thing. So how much information can we get from this name for the new thing called the interface callback? The first is the interface, what is the interface? A friend who believes in the basics of programming knows that it is a specification, but this specification has no effect until it is implemented. Just like a job advertisement, the conditions listed on the ad are broad, such as I want to recruit a programmer who is familiar with Java. This is a specification, you want to apply to meet the requirements of the familiar Java, but to meet the conditions of the situation may vary widely, may be male, can also have a female, may be a master, may also just meet this condition. But as long as this condition is met, all can come to apply, can be called an applicant. Here, the recruitment condition is the interface, the candidate is realized. So how does the callback understand? Callback-back to call, back to where, back to the definition of the place, back to the implementation of the place, then this is not an indirect statement of the fact that I implement the interface and where I call the interface is not the same place. The question is not clear.The interface callback is I have a function, but this function I do not, but have an entity it has this function, I can through its function to realize my function.
Is it still dizzy? Yes, then we'll go down. For example, in the document editor there will be a general print this function, I call the printing function, if it does not provide a working printer, it is not a way to work. But when we pick up the printer, and then print this function, the print content appears in the printer exit, printing function completed. But if I do not call this print function in the document editor, is it not that there will be no printing results, which is not very magical.This is the interface callback charm, function I have but really do not do me, but you want to do things can not without me.
So what's the point of doing that? For example, if I need to paint a house, but I can't paint myself, what can I do? It must be someone to paint, not a painter to ask me if I need to paint the house. Because the time of the painting is uncertain, there is no need to paint every moment. After we judge by various circumstances, at the right time, we ask the painter to finish our painting task. When we do not need to paint, the painting workers can serve others, so as to improve the efficiency of the painting workers, and not affect my painting task.so the meaning of the interface callback is very clear, I command what I want to do, but I do not work, we are from the executive's identity to the conductor, so as to facilitate the organization of our functions, without wasting a lot of valuable resources.
Say long time, so how should we implement Interface callbacks? In the case of the whitewashed house above, I wrote a small test program.
1 Package andy.example; 2 3 Public Interface Paint {4 // Painting Task 5 void paint (); 6 }
Here, we define an interface in which there is only one method of painting.
1 Packageandy.example;2 3 //worker entity, with stucco function4 Public classWorkerImplementspaint{5 6 @Override7 Public voidpaint () {8System.out.println ("I'm a painter and I'm a good painter!") ");9 }Ten One}
A whitewashed worker with a really whitewashed function.
1 Packageandy.example;2 3 //entities that may need to paint the house4 Public classAsker {5 6 //Do you need paint ?7 Private BooleanIsneedpaint;8 9 Public voidSetisneedpaint (Booleanisneedpaint) {Ten This. Isneedpaint =Isneedpaint; One } A - /** - * Command Paint the * - * @paramworker - * Painting Workers - */ + Public voiddopaint (Paint paint) { - if(isneedpaint) { + paint.paint (); A}Else { atSystem.out.println ("The house doesn't need painting, you're kidding me!") "); - } - } -}
Someone who might need a painting.
1 Packageandy.example;2 3 //Test Class4 Public classTest {5 6 Public Static voidMain (string[] args) {7 //There are two requestors8Asker asker1=NewAsker ();9Asker asker2=NewAsker ();Ten One //The second one really needs painting . AAsker2.setisneedpaint (true); - - //The same whitewashed worker . theWorker worker=NewWorker (); - -System.out.println ("The requestor who does not need to paint the house, after invoking the Paint method"); - Asker1.dopaint (worker); +System.out.println ("The requestor who needs to paint the house, after invoking the painting method"); - Asker2.dopaint (worker); + } A at}
The Test class.
No doubt the result.
Simple analysis: People who need to paint according to the situation to determine whether the house needs painting, when no need to paint to call the painting function is not valid, such as the result of a person who really need to paint, at the appropriate time to call the painting method, complete the painting work.
Joke Interface callback