c语言和java如何实现闭包?

js go python 实现闭包貌似很简单

呼唤远方
浏览 1257回答 6
6回答

www说

现代的Java已经支持闭包了。标准的C不支持闭包。gcc和clang倒是有blocks扩展。

尚方宝剑之说

C语言实现闭包函数,可以参考《C语言接口与实现》一书,作者多次使用闭包,不过作者说的闭包应该指的是回调函数。和Java估计不大相同。比如下面的这个函数Table_map,其中的回调函数apply,可以在C中称为一个闭包。注:以上是作者的书中,中译本的说法~void Table_map(T table,&nbsp; &nbsp; void apply(const void *key, void **value, void *cl),&nbsp; &nbsp; void *cl) {&nbsp; &nbsp; int i;&nbsp; &nbsp; unsigned stamp;&nbsp; &nbsp; struct binding *p;&nbsp; &nbsp; assert(table);&nbsp; &nbsp; assert(apply);&nbsp; &nbsp; stamp = table->timestamp;&nbsp; &nbsp; for (i = 0; i < table->size; i++)&nbsp; &nbsp; &nbsp; &nbsp; for (p = table->buckets[i]; p; p = p->link) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; apply(p->key, &p->value, cl);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; assert(table->timestamp == stamp);&nbsp; &nbsp; &nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP