In C and C ++, function passing can be implemented through function pointers. In C #, function transfer can be implemented through delegation, Action, and Func. In Java, there are no function pointers and no delegation. How should the functions be passed?
You can use either of the following methods.
1. Use handler to instantiate Class B in Class A and pass A handler to Class B. B sends A message through handler. After receiving the message in A, execute the corresponding function. Strictly speaking, this is not a function transfer, but a disguised function call.
2. Implemented through interfaces. The following is the implementation code.
ICallback. java
Package com. sparkle. sgmjl; public interface ICallback {abstract void callback (String data );}
Pass ICallback to Class B in Class A. The Code is as follows.
ClassA. java
ClassA classA = new ClassA (new ICallback () {@ Overridepublic void callback (String data) {Test (data) ;}}); private void Test (String data) {Log. e ("TEST", data ))}
ClassB. java
Public ClassB (ICallback icallback) {icallback ("Call OK ");}
The preceding notes are for future use.