Still don't understand the callback mechanism? Callback? What the heck? Why did you make that? Is it used for hair?
In fact, as the name implies, the callback is "back to call each other." That is: When I contact you, you turn back to contact me. Yes, that's right, the Hollywood rule--"Don t call me; I ' ll call you. "
But here's a little bit of detail, "Don t bother me, if you're really good, I'll call you, but firstly,you should tell me yournumber"
Is this kind of explanation a little bit clear? Don't understand? It's okay, keep going down.
There are too many blog posts on the callback mechanism, and you must know that the callback is "" A method called "Class C" in Class A, and then the Class B in turn calls Method D "" in class Blahblah
If A is an actor, B is Hollywood, then actor a calls Hollywood B and Hollywood B in turn contacts actor a.
But! The first call to a calls B, in fact, is to let a ah. Still don't understand?
Scenario Integration Type:
-----------------------------------------below is the background----------------------------------------------
Hollywood is very bull, the actors want to enter, but,Hollywood management is very normative, have their own set of standards "see Clearly, this standard is b formulated, to B is known ", All the actors who want to come in must obey this set of standards (interface) "Class A must obey this standard"
Interface actorstandard{
void culture ();
.....
}
---------------------------------The following is the first call---------------------------------------
A: "Hello?" Is it Hollywood? I am actor A, I call you now, this is my mobile phone number, I am very cow oh, and meet your Standards Oh "(Implement Interface Actorstandard)
B: "OK, I've got your number, I'll see your work, and I'll contact you if it's really cool."
---------------------------------The following is the second call----------------------------------------
B: "Is it a?" You are really a cow, we accept you. Next, we will wrap you (call a in the interface method implementation), promote you (call a in the interface method implementation) ... ”
--------------------------------Callback is complete-----------------------------------------
Do you understand? The first call to a B is precisely to let B call a (the method in),
So here's the question:
Where is the meaning of the first call?
A: Because B only defines the specification, does not know the existence of a! The first Call makes B aware of the existence of a, so this call is primarily a registration of a information. So the function that is called for the first time can also be called the registration function.
If you do not use callbacks, can you use a direct call?
A: This is not possible, first we look at the relationship between the three classes/interfaces.
B is a standard actorstandard, so so far, Actorstandard is visible to B, but actor A is invisible to B.
So, Class B, if you want to interact with Actorstandard, it must include a actorstandard, but because Actorstandard is only an interface, so its implementation class can only be passed in from outside .
Actor a must conform to the Actorstandard standard, so actorstandard is visible to a. And actor A has to go to B to register first, so B is also visible to a.
Therefore, a class to implement the interface of Actorstandard, followed by Class A to have Class B objects in order to register their own information in B.
Why a must go to B where to register?
A: Above said, B only know actorstandard this interface, if a This implementation class to B has been transparent, then how to interact between the two?
Therefore, the structure should be like this.
1 Packagetest;2 3 Interfaceactorstandard{4 voidBringup ();5 String getName ();6 }7 8 classActorImplementsactorstandard{9 PrivateHollywood Hollywood;Ten PrivateString name; One //Although this method is in the actor class, but is often provided to the Hollywood call, itself does not call A //why not? - //because the actor just achieved the standard AH, this standard is Hollywood. The performer of the action is also Hollywood. - //own methods, let others invoke, and be their own callers in turn call their own methods, so called callbacks "do not know can understand" the Public voidBringup () { -SYSTEM.OUT.PRINTLN ("Prepare to cultivate ....")); -System.out.println ("Wrap first ..."); -System.out.println ("Another hype ..."); + } - PublicString GetName () { + returnname; A } at PublicActor (Hollywood Hollywood, String name) { - This. Hollywood =Hollywood; - This. Name =name; - } - Public voidCall () {//First Call, complete registration -System.out.println ("I am" + name + ", I am gona call Hollywood"); inHollywood.register ( This); - } to } + - classHollywood { the Public voidRegister (Actorstandard actor) {//Registration Complete *System.out.println ("This is Hollywood,i received a phone"); $System.out.println ("Oh, now I know the" This call is from "+actor.getname ());Panax NotoginsengSystem.out.println ("He is purly good,i am gonna to bring up with him"); -Actor.bringup ();//the second call, the reverse call. The so-called callback the } + } A the Public classCallbacktest { + Public Static voidMain (string[] args) { -Hollywood Hollywood =NewHollywood (); $Actor Hughjackman =NewActor (Hollywood, "Hugh Jackman"); $ Hughjackman.call (); - } -}
The results of the operation are as follows:
I am Hugh Jackman, I am gona call Hollywood
This is Hollywood,i received a phone
Oh, now I know the is from Hugh Jackman
He is purly good,i am gonna to bring up with him
Prepare to cultivate ....
Wrap it up first ...
Another hype ...
If you do not understand, then I really do not have the means. I found a lot of articles on the Internet, but basically did not explain the interface and the relationship between Class B, so look down or foggy, as to what is the use of callbacks? Can be combined with the asynchronous view, please see this blog post, and this article, and this article. I have not seen this article, should also be good.
This article is purely tonight to understand, want to strike a record, there must be understanding deviation, logic, such as flawed problems. Take time to refine it later.
If you do not understand, sorry to waste everyone's time, you can leave a message to discuss.
"Original" callback mechanism