- Groovy 教程
- Groovy - 主页
- Groovy - 概述
- Groovy - 环境
- Groovy - 基本语法
- Groovy - 数据类型
- Groovy - 变量
- Groovy - 运算符
- Groovy - 循环
- Groovy - 决策
- Groovy - 方法
- Groovy - 文件 I/O
- Groovy - 可选
- Groovy - 数字
- Groovy - 字符串
- Groovy - 范围
- Groovy - 列表
- Groovy - 地图
- Groovy - 日期和时间
- Groovy - 正则表达式
- Groovy - 异常处理
- Groovy - 面向对象
- Groovy - 泛型
- Groovy - 特征
- Groovy - 闭包
- Groovy - 注释
- Groovy-XML
- Groovy-JMX
- Groovy - JSON
- Groovy-DSLS
- Groovy - 数据库
- Groovy - 构建者
- Groovy - 命令行
- Groovy - 单元测试
- Groovy - 模板引擎
- Groovy - 元对象编程
- Groovy 有用的资源
- Groovy - 快速指南
- Groovy - 有用的资源
- Groovy - 讨论
Groovy - 闭包
闭包是一个简短的匿名代码块。它通常只跨越几行代码。方法甚至可以将代码块作为参数。他们本质上是匿名的。
以下是一个简单闭包的示例及其外观。
class Example {
static void main(String[] args) {
def clos = {println "Hello World"};
clos.call();
}
}
在上面的示例中,代码行 - {println "Hello World"} 被称为闭包。该标识符引用的代码块可以通过call语句执行。
当我们运行上面的程序时,我们将得到以下结果 -
Hello World
闭包中的形式参数
闭包还可以包含形式参数,以使它们更有用,就像 Groovy 中的方法一样。
class Example {
static void main(String[] args) {
def clos = {param->println "Hello ${param}"};
clos.call("World");
}
}
在上面的代码示例中,请注意 ${param } 的使用,它会导致闭包采用参数。当通过 clos.call 语句调用闭包时,我们现在可以选择将参数传递给闭包。
当我们运行上面的程序时,我们将得到以下结果 -
Hello World
下一个插图重复前面的示例并产生相同的结果,但显示可以使用引用它的隐式单个参数。这里“it”是 Groovy 中的关键字。
class Example {
static void main(String[] args) {
def clos = {println "Hello ${it}"};
clos.call("World");
}
}
当我们运行上面的程序时,我们将得到以下结果 -
Hello World
闭包和变量
更正式地说,闭包可以在定义闭包时引用变量。以下是如何实现这一目标的示例。
class Example {
static void main(String[] args) {
def str1 = "Hello";
def clos = {param -> println "${str1} ${param}"}
clos.call("World");
// We are now changing the value of the String str1 which is referenced in the closure
str1 = "Welcome";
clos.call("World");
}
}
在上面的示例中,除了向闭包传递参数之外,我们还定义了一个名为 str1 的变量。闭包还接受变量和参数。
当我们运行上面的程序时,我们将得到以下结果 -
Hello World Welcome World
在方法中使用闭包
闭包也可以用作方法的参数。在 Groovy 中,许多数据类型(例如列表和集合)的内置方法都将闭包作为参数类型。
以下示例显示了如何将闭包作为参数发送到方法。
class Example {
def static Display(clo) {
// This time the $param parameter gets replaced by the string "Inner"
clo.call("Inner");
}
static void main(String[] args) {
def str1 = "Hello";
def clos = { param -> println "${str1} ${param}" }
clos.call("World");
// We are now changing the value of the String str1 which is referenced in the closure
str1 = "Welcome";
clos.call("World");
// Passing our closure to a method
Example.Display(clos);
}
}
在上面的例子中,
我们正在定义一个名为 Display 的静态方法,它将闭包作为参数。
然后,我们在 main 方法中定义一个闭包,并将其作为参数传递给 Display 方法。
当我们运行上面的程序时,我们将得到以下结果 -
Hello World Welcome World Welcome Inner
集合和字符串中的闭包
一些 List、Map 和 String 方法接受闭包作为参数。让我们看一下如何在这些数据类型中使用闭包的示例。
将闭包与列表一起使用
以下示例展示了如何将闭包与列表一起使用。在下面的示例中,我们首先定义一个简单的值列表。然后列表集合类型定义一个名为 的函数。每个. 该函数采用闭包作为参数,并将闭包应用于列表的每个元素。
class Example {
static void main(String[] args) {
def lst = [11, 12, 13, 14];
lst.each {println it}
}
}
当我们运行上面的程序时,我们将得到以下结果 -
11 12 13 14
将闭包与地图一起使用
以下示例展示了如何将闭包与 Map 一起使用。在下面的示例中,我们首先定义一个简单的键值项映射。然后,地图集合类型定义一个名为 .each 的函数。该函数采用闭包作为参数,并将闭包应用于映射的每个键值对。
class Example {
static void main(String[] args) {
def mp = ["TopicName" : "Maps", "TopicDescription" : "Methods in Maps"]
mp.each {println it}
mp.each {println "${it.key} maps to: ${it.value}"}
}
}
当我们运行上面的程序时,我们将得到以下结果 -
TopicName = Maps TopicDescription = Methods in Maps TopicName maps to: Maps TopicDescription maps to: Methods in Maps
通常,我们可能希望迭代集合的成员,并仅当元素满足某些条件时才应用某些逻辑。这可以通过闭包中的条件语句轻松处理。
class Example {
static void main(String[] args) {
def lst = [1,2,3,4];
lst.each {println it}
println("The list will only display those numbers which are divisible by 2")
lst.each{num -> if(num % 2 == 0) println num}
}
}
上面的示例显示了闭包中使用的条件 if(num % 2 == 0) 表达式,该表达式用于检查列表中的每个项目是否能被 2 整除。
当我们运行上面的程序时,我们将得到以下结果 -
1 2 3 4 The list will only display those numbers which are divisible by 2. 2 4
与闭包一起使用的方法
闭包本身提供了一些方法。