I saw that the explanations of some callbacks on the Internet were very complicated, especially custom callbacks based on Android, and I felt confused. So I also wrote this article based on my explanation of callbacks.
Let’s take a look at a simple example:
There are two classes ClassA, and ClassB. ClassA calls the methods in ClassB.
public class ClassB { public void method_from_classB(){ for(int i=0;i<10;i++) ("..."+i); } } public class ClassA { public static void main(String args[]){ ClassB classB = new ClassB(); classB.method_from_classB(); } }
Output:
...0...1...2...3...4...5...6...7...8...9
Damn, which idiot wrote a blog post insulting my IQ, isn't it? Hehehe, it's for comparison. Let's take a look at how ClassA calls the method in ClassB. Note that it is the callback:
Let ClassB implement ClassA-defined interfaces
public class ClassB implements { public ClassB(){ new ClassA().RegisterInterface(this); ("...ClassB..."+this); } @Override public void method_from_interface() { for(int i=0;i<10;i++) ("..."+i); } /* public void method_from_classB(){ for(int i=0;i<10;i++) ("..."+i); }*/ }
ClassA defines interfaces and abstract methods:
public class ClassA { public static ClassAInterface classAInterface; public interface ClassAInterface{ public void method_from_interface(); } public void RegisterInterface(ClassAInterface a_interface){ = a_interface; ("...a_interface..."+a_interface); } public static void main(String args[]){ ClassB classB = new ClassB();// Tag @1, explain at the end //classB.method_from_classB(); ("...classAInterface..."+classAInterface); if(classAInterface != null){ classAInterface.method_from_interface(); } } }
Output:
...0...1...2...3...4...5...6...7...8...9
To sort it out, that is, I defined an interface in ClassA, and another method is defined in the interface, but without the method body, I will not do anything.
When ClassA executes the mian() function, it will call the interface method. However, as mentioned earlier, the interface method does not implement specific things, and it will find the corresponding methods in ClassB to implement specific things.
Yoyo, how to find the ClassA interface method? Will it go to heaven? ? ?
That is, the following analysis of how this code goes to heaven:
// Use the interface callback to implement the method in ClassB.
classAInterface.method_from_interface();
I used to print out the log for analysis in the above code:
The first one (method in ClassA):
public void RegisterInterface(ClassAInterface a_interface){ = a_interface; ("...a_interface..."+a_interface); }
Output:
...a_interface...ClassB@3ddb8962
The second one:
public ClassB(){ new ClassA().RegisterInterface(this); ("...ClassB..."+this); }
Output:
...ClassB...ClassB@3ddb8962
The third one:
("...classAInterface..."+classAInterface); if(classAInterface != null){ classAInterface.method_from_interface(); }
Output:
...classAInterface...ClassB@3ddb8962
Did you suddenly realize it when you saw this? The output is all "ClassB@3ddb8962" which is a reference to the ClassB object! ! !
ah! The interface is just passing a reference to the ClassB object to ClassA. Is this statement that will go to heaven easy to explain?
classAInterface.method_from_interface();
Equivalent to [email protected]_from_interface();
This is the code that comes to the top:
ClassB classB = new ClassB(); classB.method_from_classB();
The same is true, which is why I first asked this example! ! !
I believe that after seeing this, I should understand what the callback of the interface is going on.
But one thing is a bit confused. Why is it so troublesome to have an interface callback? The top one is executed in ClassA:
ClassB classB = new ClassB(); classB.method_from_classB();
It is not possible to call the method in ClassA in ClassB. . . . But if ClassA wants to call ClassC, ClassD..., and the methods inside, do you also need to change the code in ClassA and instantiate the objects of ClassC, ClassD..., which is obviously not good. If you use the interface, you don’t need to change the code in ClassA. Any class can just implement the interface in ClassA.
Explain tag @1:
The above paragraph seems to violate the tag @1, and it is indeed necessary to instantiate the ClassB object in ClassA.
Because you need to execute the code in the constructor when initializing:
public ClassB(){ // It is equivalent to registering a callback event. Beginners may forget to "register" when they appear as empty callback pointers. new ClassA().RegisterInterface(this); }
Passing this to ClassA is the function of using log analysis above.
But in Android development, you don’t have to save this.
Can be executed when the activity is initialized:
@Override protected void onCreate(Bundle savedInstanceState) { // It is equivalent to registering a callback event. Beginners may forget to "register" when they appear as empty callback pointers. new ClassA().RegisterInterface(this); }
In Android development, the mian() function in ClassA can be replaced by events and triggered:
like:
(new OnClickListener() { @Override public void onClick(View v) { // TODO automatically generates method stub if(classAInterface != null){ classAInterface.method_from_interface(); } });