Interfaces for Callbacks

A common technique in the C language is to pass a pointer to a function in the argument list of another function. The receiving function can invoke the passed function using the pointer. This approach is referred to as a callback.

通常在C中通过指针作为函数参数传递给其他函数,被调用的函数可以调用传递过来的指针来实现回调。
for example:

void  callback( void ) Interfaces for Callbacks(Java中回调) {
   //todo:write your code in here.
   //回调时要做的事
Interfaces for Callbacks(Java中回调)   
Interfaces for Callbacks(Java中回调)}

Interfaces for Callbacks(Java中回调)typedef   void 
(*)( void ) _callback;  //定义类型
Interfaces for Callbacks(Java中回调)
void  AnotherProc(_callback Callbacked)
Interfaces for Callbacks(Java中回调) {
Interfaces for Callbacks(Java中回调)    Callbacked();
Interfaces for Callbacks(Java中回调)}

Interfaces for Callbacks(Java中回调)
Interfaces for Callbacks(Java中回调)
void  main( void )
Interfaces for Callbacks(Java中回调) {
Interfaces for Callbacks(Java中回调)    AnotherProc(callback);
Interfaces for Callbacks(Java中回调)}


Callbacks are very useful in situations where you want to invoke different functions without needing to know what particular function is being invoked. For example, a plotting function could receive a pointer to a function that takes a value on the horizontal axis as an argument and returns a value for the vertical axis. The plotting function could then plot any such function that is passed to it.

Java, however, does not provide for pointers (direct memory addresses) to methods. Instead, interfaces are used for callbacks. In this case, a method holds an interface reference in its argument list and then invokes a method in the interface.

(The Method class in the Reflection package can represent a method and can invoke it in a general manner. However, it is not recommended to use Method for callbacks. It is rather clumsy and it also loses the type checking on the arguments and return type during the compilation.)

In the following code, we see that the aFunc(Switchable obj) method invokes the getState() method of the Switchable interface. An instance of any class that implements the Switchable interface can be passed, thus providing the same generality as the pointer callbacks in C.

 1 Interfaces for Callbacks(Java中回调)public   class  TestCallBack
 2 Interfaces for Callbacks(Java中回调)
 3 Interfaces for Callbacks(Java中回调)   public   static   void  main(String [] args)
 4    Interfaces for Callbacks(Java中回调) {
 5 Interfaces for Callbacks(Java中回调)    Switchable []switches  =   new  Switchable[ 2 ];
 6 Interfaces for Callbacks(Java中回调)    switches[ 0 =   new  Relay();
 7 Interfaces for Callbacks(Java中回调)    switches[ 1 =   new  Relay();
 8 Interfaces for Callbacks(Java中回调)    switches[ 2 =   new  Valve();;
 9 Interfaces for Callbacks(Java中回调)
10 Interfaces for Callbacks(Java中回调)     for int  i = 0 ; i < 3 ; i ++ )
11      Interfaces for Callbacks(Java中回调)
12 Interfaces for Callbacks(Java中回调)      aFunc(switches[i]);
13 Interfaces for Callbacks(Java中回调)    }
 
14 Interfaces for Callbacks(Java中回调)  }

15 Interfaces for Callbacks(Java中回调)   //  Pass Switchable objects and call their getState() 
16 Interfaces for Callbacks(Java中回调)    void  aFunc( Switchable obj)
17    Interfaces for Callbacks(Java中回调)
18 Interfaces for Callbacks(Java中回调)       if (obj.getState()) doSomething();
19 Interfaces for Callbacks(Java中回调)  }

20 Interfaces for Callbacks(Java中回调)  Interfaces for Callbacks(Java中回调) other codeInterfaces for Callbacks(Java中回调)
21 Interfaces for Callbacks(Java中回调)}
 
22 Interfaces for Callbacks(Java中回调)
See previous example for the Switchable,
Relay, & Valve definitions.

相关文章:

  • 2021-04-03
  • 2021-10-05
  • 2021-06-24
  • 2021-09-25
  • 2021-12-25
  • 2021-07-06
  • 2021-12-14
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-17
  • 2022-01-18
  • 2021-12-30
  • 2021-09-08
相关资源
相似解决方案