There are three ways to call between modules:
1. Synchronous call
The synchronous invocation is simple, calling method B directly, and must wait until method a executes before proceeding with the original program.
2. Asynchronous invocation
An asynchronous invocation is an implementation of program B in a program, and when B runs, it can invoke the implementation in the a program when it satisfies the condition. Examples Show
Public Interfacefood{voideat ();} Public classCowImplementsfood{@overridevoideat () {SYSO ("Eat cow"); } } Public classtiger{PrivateFood and food ; Public voidSetfood (food food) { This. Food =Food ; } voidhungry () {SYSO ("Hungry"); if (eat!=null) food.eat (); } Public void StaticMain (string[] args) {Tiger Tiger=NewTiger (); Tiger.setfood (NewCow ()); Tiger.hungry (); } }
The tiger is hungry, if there is food can eat it, now the cow is equivalent to program A to achieve this interface,
The tiger is the equivalent of program B, which contains this interface,
Now send the cow to the mouth of the tiger, is Setfood
The tiger is hungry to eat cattle, this time called a program implementation of this interface.
3. Callback
The Android program will definitely be familiar, the callback is actually the above two combination.
For example, we are instantiating a button class, it will set a onclicklistener, which is equivalent to the above asynchronous call, but this asynchronous call is triggered by the button itself, button of the PerformClick () The simulated click event then executes the custom Onclicklistener.
Simple parsing of three Java call modes-synchronous, asynchronous, callback