博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Interfaces for Callbacks(Java中回调)
阅读量:6208 次
发布时间:2019-06-21

本文共 2236 字,大约阅读时间需要 7 分钟。

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:

ExpandedBlockStart.gif
ContractedBlock.gif
void
 callback(
void
)
dot.gif
{
   //todo:write your code in here.
   //回调时要做的事
InBlock.gif   
ExpandedBlockEnd.gif}
None.giftypedef   void 
(*)(
void
) _callback;  //定义类型
None.gif
void
 AnotherProc(_callback Callbacked)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
{
InBlock.gif    Callbacked();
ExpandedBlockEnd.gif}
None.gif
None.gif
void
 main(
void
)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
{
InBlock.gif    AnotherProc(callback);
ExpandedBlockEnd.gif}

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 class in the 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
None.gif
public
 
class
 TestCallBack
 2
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
 3 InBlock.gif   public   static   void  main(String [] args)
 4 ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif {
 5 InBlock.gif    Switchable []switches  =   new  Switchable[ 2 ];
 6 InBlock.gif    switches[ 0 =   new  Relay();
 7 InBlock.gif    switches[ 1 =   new  Relay();
 8 InBlock.gif    switches[ 2 =   new  Valve();;
 9 InBlock.gif
10 InBlock.gif     for int  i = 0 ; i < 3 ; i ++ )
11 ExpandedSubBlockStart.gifContractedSubBlock.gif     dot.gif
12 InBlock.gif      aFunc(switches[i]);
13 ExpandedSubBlockEnd.gif    }
 
14 ExpandedSubBlockEnd.gif  }
15 InBlock.gif   //  Pass Switchable objects and call their getState() 
16 InBlock.gif    void  aFunc( Switchable obj)
17 ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif
18 InBlock.gif       if (obj.getState()) doSomething();
19 ExpandedSubBlockEnd.gif  }
20 InBlock.gif  dot.gif other codedot.gif
21 ExpandedBlockEnd.gif}
 
22
None.gif
See previous example for the Switchable,
Relay, & Valve definitions.
你可能感兴趣的文章
go语言入门教程百度网盘:椭圆曲线加密算法ECC和椭圆曲线数字签名算法ECDSA
查看>>
使用Nginx搭建网页服务器
查看>>
大部分程序员都在抱怨自己工资低,但是真的工资低吗?
查看>>
Android 音视频开发 - 使用Camera采集视频
查看>>
探索iOS中Block的实现原理
查看>>
记录一次线上OOM情况排查过程
查看>>
91 Decode Ways
查看>>
工作中遇到的问题
查看>>
含着泪做Swift3迁移
查看>>
微信小程序海报生成组件封装
查看>>
AI伦理无法回避的5个问题:生物进化是否有方向?
查看>>
一半人将因人工智能失业?麻省理工科学家表示太可笑!
查看>>
‘生逢其时’的文化IP该如何借力科技?_创成汇
查看>>
java B2B2C Springcloud电子商城系统--------负载均衡(Load Balance)
查看>>
Java springcloud B2B2C o2o多用户商城 springcloud架构 (一)服务的注册与发现(Eureka)...
查看>>
手把手教你写电商后台管理系统(七) - 用户模块
查看>>
比特币、比特币现金的下一步是什么?Craig Wright博士说,可替代现金
查看>>
字节码执行引擎-类加载及执行子系统的案例与实战
查看>>
微软整合实验(三):AD域环境的搭建,基于Server 2008 R2
查看>>
Linux 用户管理
查看>>