课程表

Groovy课程

工具箱
速查手册

Groovy 闭包

当前位置:免费教程 » Java相关 » Groovy

闭包是一个短的匿名代码块。它通常跨越几行代码。一个方法甚至可以将代码块作为参数。它们是匿名的。

下面是一个简单闭包的例子,它是什么样子。

  1. class Example {
  2. static void main(String[] args) {
  3. def clos = {println "Hello World"};
  4. clos.call();
  5. }
  6. }

在线运行实例

在上面的例子中,代码行 - {println“Hello World”}被称为闭包。此标识符引用的代码块可以使用call语句执行。

当我们运行上面的程序,我们将得到以下结果 -

  1. Hello World

闭包中的形式参数

闭包也可以包含形式参数,以使它们更有用,就像Groovy中的方法一样。

  1. class Example {
  2. static void main(String[] args) {
  3. def clos = {param->println "Hello ${param}"};
  4. clos.call("World");
  5. }
  6. }

在线运行实例

在上面的代码示例中,注意使用$ {param},这导致closure接受一个参数。当通过clos.call语句调用闭包时,我们现在可以选择将一个参数传递给闭包。

当我们运行上面的程序,我们将得到以下结果 -

  1. Hello World

下一个图重复了前面的例子并产生相同的结果,但显示可以使用被称为它的隐式单个参数。这里的'it'是Groovy中的关键字。

  1. class Example {
  2. static void main(String[] args) {
  3. def clos = {println "Hello ${it}"};
  4. clos.call("World");
  5. }
  6. }

在线运行实例

当我们运行上面的程序,我们将得到以下结果 -

  1. Hello World

闭包和变量

更正式地,闭包可以在定义闭包时引用变量。以下是如何实现这一点的示例。

  1. class Example {
  2. static void main(String[] args) {
  3. def str1 = "Hello";
  4. def clos = {param -> println "${str1} ${param}"}
  5. clos.call("World");
  6. // We are now changing the value of the String str1 which is referenced in the closure
  7. str1 = "Welcome";
  8. clos.call("World");
  9. }
  10. }

在线运行实例

在上面的例子中,除了向闭包传递参数之外,我们还定义了一个名为str1的变量。闭包也接受变量和参数。

当我们运行上面的程序,我们将得到以下结果 -

  1. Hello World
  2. Welcome World

在方法中使用闭包

闭包也可以用作方法的参数。在Groovy中,很多用于数据类型(例如列表和集合)的内置方法都有闭包作为参数类型。

以下示例显示如何将闭包作为参数发送到方法。

  1. class Example {
  2. def static Display(clo) {
  3. // This time the $param parameter gets replaced by the string "Inner"
  4. clo.call("Inner");
  5. }
  6. static void main(String[] args) {
  7. def str1 = "Hello";
  8. def clos = { param -> println "${str1} ${param}" }
  9. clos.call("World");
  10. // We are now changing the value of the String str1 which is referenced in the closure
  11. str1 = "Welcome";
  12. clos.call("World");
  13. // Passing our closure to a method
  14. Example.Display(clos);
  15. }
  16. }

在线运行实例

在上述示例中,

  • 我们定义一个名为Display的静态方法,它将闭包作为参数。

  • 然后我们在我们的main方法中定义一个闭包,并将它作为一个参数传递给我们的Display方法。

当我们运行上面的程序,我们将得到以下结果 -

  1. Hello World
  2. Welcome World
  3. Welcome Inner

集合和字符串中的闭包

几个List,Map和String方法接受一个闭包作为参数。让我们看看在这些数据类型中如何使用闭包的例子。

使用闭包和列表

以下示例显示如何使用闭包与列表。在下面的例子中,我们首先定义一个简单的值列表。列表集合类型然后定义一个名为.each的函数。此函数将闭包作为参数,并将闭包应用于列表的每个元素

  1. class Example {
  2. static void main(String[] args) {
  3. def lst = [11, 12, 13, 14];
  4. lst.each {println it}
  5. }
  6. }

在线运行实例

当我们运行上面的程序,我们将得到以下结果 -

  1. 11
  2. 12
  3. 13
  4. 14

使用映射闭包

以下示例显示了如何使用闭包。在下面的例子中,我们首先定义一个简单的关键值项Map。然后,映射集合类型定义一个名为.each的函数。此函数将闭包作为参数,并将闭包应用于映射的每个键值对。

  1. class Example {
  2. static void main(String[] args) {
  3. def mp = ["TopicName" : "Maps", "TopicDescription" : "Methods in Maps"]
  4. mp.each {println it}
  5. mp.each {println "${it.key} maps to: ${it.value}"}
  6. }
  7. }

在线运行实例

当我们运行上面的程序,我们会得到以下结果 -

  1. TopicName = Maps
  2. TopicDescription = Methods in Maps
  3. TopicName maps to: Maps
  4. TopicDescription maps to: Methods in Maps

通常,我们可能希望遍历集合的成员,并且仅当元素满足一些标准时应用一些逻辑。这很容易用闭包中的条件语句来处理。

  1. class Example {
  2. static void main(String[] args) {
  3. def lst = [1,2,3,4];
  4. lst.each {println it}
  5. println("The list will only display those numbers which are divisible by 2")
  6. lst.each{num -> if(num % 2 == 0) println num}
  7. }
  8. }

在线运行实例

上面的例子显示了在闭包中使用的条件if(num%2 == 0)表达式,用于检查列表中的每个项目是否可被2整除。

当我们运行上面的程序,我们会得到以下结果 -

  1. 1
  2. 2
  3. 3
  4. 4
  5. The list will only display those numbers which are divisible by 2.
  6. 2
  7. 4

闭包使用的方法

闭包本身提供了一些方法。

序号方法和描述
1find()

find方法查找集合中与某个条件匹配的第一个值。

2findAll()

它找到接收对象中与闭合条件匹配的所有值。

3any() & every()

方法any迭代集合的每个元素,检查布尔谓词是否对至少一个元素有效。

4collect()

该方法通过集合收集迭代,使用闭包作为变换器将每个元素转换为新值。

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号