- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我了解闭包,并在某些语言(例如 Python 和 SML)中应用过。然而,当我阅读有关 Java 闭包的维基百科时(当然,只有 8 个版本),我不明白 Java 在他们的示例中是否支持闭包的区别。
我从维基百科复制的那些代码:Closure
没有闭包的java代码:
class CalculationWindow extends JFrame {
private volatile int result;
...
public void calculateInSeparateThread(final URI uri) {
// The expression "new Runnable() { ... }" is an anonymous class implementing the 'Runnable' interface.
new Thread(
new Runnable() {
void run() {
// It can read final local variables:
calculate(uri);
// It can access private fields of the enclosing class:
result = result + 10;
}
}
).start();
}
}
如果 Java 支持闭包,代码将如下所示:
class CalculationWindow extends JFrame {
private volatile int result;
...
public void calculateInSeparateThread(final URI uri) {
// the code () -> { /* code */ } is a closure
new Thread(() -> {
calculate(uri);
result = result + 10;
}).start();
}
}
所以,我的问题是:如果 Java 支持闭包,那么第二个代码中有什么特别之处?我真的看不出两个代码之间的主要区别。
请告诉我这一点。
谢谢 :)
最佳答案
重点是它们在功能上并没有太大的不同:
() -> {
calculate(uri);
result = result + 10;
}
等效于 Runnable 的新类实例,具有 run() 方法的等效实现。您用一个简单的 lambda 函数替换了大量“样板”代码。
使用 lambda,您的代码将变得更具表现力、更简洁、更易于编写且可读性更强。一旦您进入闭包和 lambda 函数的世界,这就是好处的开始。
关于Java : explain Closure in this code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14784577/
我刚刚开始使用 Google Closure 做一些工作,我想将选择字段的创建移动到它自己的模板中,并使用类似以下内容调用该模板: {call templates.utils.select} {p
我有一些代码,简化后看起来像: fn foo() -> Vec { unsafe { unsafe_iterator().map(|n| wrap_element(n)).co
我正在从脚本内部调用Closure Compiler(closurecompiler.jar)。该脚本还生成Closure Compiler需要编译的一些javascript。有没有办法将此JavaS
以下示例代码生成有关高级优化的编译器警告:“JSC_UNSAFE_NAMESPACE:为命名空间 NS 创建的别名不完整”。如果我删除@enum 注释,它不会发出警告。 var NS = {}; /*
看代码: let add_one = |&: x| { 1 + x }; 我知道x是闭包参数,但是闭包中的&:是什么意思? 最佳答案 这是 Rust 的一个文档不足的部分(并且过时,请参阅评论)。我知
PHP manual for anonymous functions (即闭包)指出: Anonymous functions are currently implemented using the
我从脚本内部调用 Closure Compiler (closurecompiler.jar)。该脚本还生成了一些 Closure Compiler 需要编译的 javascript。有没有办法将这个
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 7年前关闭。 Improve t
当鼠标在文档正文中移动时,我试图调用一个函数。但是,下面的事件处理程序不起作用。 goog.events.listen(document, 'onmousemove', function(e)
我试过了 java -jar compiler.jar --js jj.js --js_output_file jj.js 输出文件大小为 0。 如果我不想从 .min.js 重命名为 .js,我该怎
Google Closure UI库如何与Google DART一起使用? 最佳答案 Dart没有使用JavaScript库的功能。这是设计使然,因为Dart旨在同时针对Dart VM和转换为JS的D
是否可以使用 Google Closure 编译器在两个文件中定义一个类?例如,如果我自动生成一个类并希望为用户输入的代码保留另一个类: 在 MyClass.AutoGenerated.js 中 go
当我在 http://closure-compiler.appspot.com 处的闭包编译器中测试以下代码时: // ==ClosureCompiler== // @output_file_name
是否可以使用 Google Closure 编译器在两个文件中定义一个类?例如,如果我自动生成一个类并希望为用户输入的代码保留另一个类: 在 MyClass.AutoGenerated.js 中 go
当我运行闭包编译器时,会收到一堆这样的警告: [exec] jquery/3.2.1/dist/jquery.js:733: WARNING - Suspicious code. The resul
假设您正在一个具有多个外部库依赖项的 javascript 项目中工作,并且想要在 ADVANCED_OPTIMIZATIONS 模式下使用 Google Closure Compiler 编译您的源
我正在为 PIXI.js 库准备 externs。我收到以下警告: js/Test.js:188: WARNING - Property position never defined on PIXI.
我最近使用 Google 的 Closure 编译器创建了一个 JavaScript 库:https://github.com/bvaughn/task-runner 我打算让这个库供那些也需要完整闭
我正在尝试自学闭包模板。我做了一个简单的文件 simple.soy: {namespace examples.simple} /** * says hello to the world * @pa
我正在将一个项目从 jQuery 迁移到 Closure。我有一些我想编译的只迁移了一半的代码。未编译的源工作正常。我想知道使用 SIMPLE_OPTIMIZATIONS 编译它的编译命令。 原始基于
我是一名优秀的程序员,十分优秀!