Some anonymous internal classes and their callback functions are often used in Android development. For example, when we set a listener for a Button, we usually implement the OnCLickListener interface and override the Onclick () method, this is the anonymous internal class and callback function. How much do you understand the Java anonymous internal class and callback function? This document provides a detailed explanation.
First, you should know that the internal class is the main additional part of the JAVA language. An internal class can be in almost any position inside a class. It can be at the same level as an instance variable, or within a method, or even part of an expression. Java internal classes are divided into member Internal classes, local internal classes, and anonymous internal classes. The following describes anonymous internal classes.
A. About callback Functions
The callback function is used in the anonymous internal class. What is the callback function?
The so-called callback means that the client program C calls A function A in service program S, and then S calls A function B in C at A certain time. For C, this B is called a callback function. For example, the window procedure function under Win32 is a typical callback function. Generally, C does not call B by itself. C provides B for S to call it, and C has to provide it. Because S does not know who the B surname provided by C is, S will agree with the interface specification (function prototype) of B ), then C tells S to use the B function through a function R of S in advance. This process is called the registration of the callback function, and R is called the registration function. The callback mechanism is used by Web Service and Java RMI to access remote server programs. The following is an example:
Programmer A writes a program (program A), which reserves the callback function interface and encapsulates the program. Programmer B wants a to call a method in program B, So he calls back the method in B through the interface in. . In C/C ++, a callback function is used. If a function is dropped, the caller must be notified of its own pointer address, but there is no pointer in JAVA. What should I do? We can use interfaces to define callback functions.
Suppose I am A programmer, and the following is my program:
[Java] public class Caller
{
Public MyCallInterface mc;
Public void setCallfuc (MyCallInterface mc)
{
This. mc = mc;
}
Public void call (){
This. mc. method ();
}
}
Public class Caller
{
Public MyCallInterface mc;
Public void setCallfuc (MyCallInterface mc)
{
This. mc = mc;
}
Public void call (){
This. mc. method ();
}
}
I also need to define an interface so that programmer B can write the program implementation interface according to my definition.
[Java] public interface MyCallInterface
{
Public void method ();
}
Public interface MyCallInterface
{
Public void method ();
}
As a result, programmer B only needs to implement this interface to achieve the purpose of callback:
[Java] public class B implements MyCallInterface
{
Public void method ()
{
System. out. println ("Callback ");
}
Public static void main (String args [])
{
Caller call = new Caller ();
Call. setCallfuc (new B ());
Call. call ();
}
}
Public class B implements MyCallInterface
{
Public void method ()
{
System. out. println ("Callback ");
}
Public static void main (String args [])
{
Caller call = new Caller ();
Call. setCallfuc (new B ());
Call. call ();
}
}
B. About anonymous internal classes
Understand what internal classes are, the role of internal classes, java polymorphism, and callback functions. There are a lot of detailed explanations on the Internet. For the sake of simplicity, I just made a brief description. The anonymous internal class is passed to the method as a function parameter. In this method, the incoming Anonymous class is accepted using the interface type and the method is called. This is a polymorphism. The principle is actually implementing callback. Because it will call methods in your anonymous internal class. The untested code below is just for demonstration.
[Java] interface InterfaceA {
String go ();
}
Class Test (){
Public void prtGo (InterfaceA ia ){
System. out. println (ia. o ());
}
Public static void main (String [] args ){
Test t = new Test ();
T. prtGo (new InterfaceA (){
Public String go (){
Return "go ";
}
});
}
}
Interface InterfaceA {
String go ();
}
Class Test (){
Public void prtGo (InterfaceA ia ){
System. out. println (ia. o ());
}
Public static void main (String [] args ){
Test t = new Test ();
T. prtGo (new InterfaceA (){
Public String go (){
Return "go ";
}
});
}
}
From Peking University-Google Android lab