什么是闭包
一个groovy闭包就像一个代码块或者方法指针,他是定义然后执行的一段代码,但是他有一些特性:隐含变量,支持自由变量,支持currying 。
我们先来看看一些例子:
1 |
def clos = { println "hello!" } |
3 |
println "Executing the Closure:" |
4 |
clos() //prints "hello!" |
在上面的例子中”hello!”是因为调用clos()函数才打印出来的,而不是在定义的时候打印出来的。
参数
闭包的参数在->之前列出,比如:
1 |
def printSum = { a, b -> print a+b } |
2 |
printSum( 5, 7 ) //prints "12" |
如果闭包的参数是少于2个的话,那么 ->是可以省略的。
Parameter notes
A Closure without -> , i.e. {} , is a Closure with one argument that is implicitly named as ‘it’. (see below for details) In some cases, you need to construct a Closure with zero arguments, e.g. using GString for templating, defining EMC Property etc. You have to explicity define your Closure as { -> } instead of just { }
You can also use varargs as parameters, refer to the Formal Guide for details. A JavaScript-style dynamic args could be simulated, refer to the Informal Guide.
自由变量
闭包能引用在参数列表中没有列出的变量,这些变量成为自由变量,他们的作用范围在他们定义的范围内:
2 |
def incByConst = { num -> num + myConst } |
3 |
println incByConst(10) // => 15 |
另外一个例子:
2 |
def localVariable = new java.util.Date() |
3 |
return { println localVariable } |
6 |
def clos = localMethod() |
8 |
println "Executing the Closure:" |
9 |
clos() //prints the date when "localVariable" was defined |
隐式变量
it
如果你有一个闭包但是只有一个参数,那么你可以省略这个参数,比如:
1 |
def clos = { print it } |
2 |
clos( "hi there" ) //prints "hi there" |
this, owner, and delegate
this : 和java中是一样的, this refers to the instance of the enclosing class where a Closure is defined
owner : the enclosing object (this or a surrounding Closure)
delegate : by default the same as owner, but changeable for example in a builder or ExpandoMetaClass
3 |
println this.class.name |
4 |
println delegate.class.name |
6 |
println owner.class.name |
12 |
def clos = new Class1().closure |
闭包作为方法参数
当一个方法使用闭包作为最后一个参数的话,那么就可以内嵌一个闭包,比如:
1 |
def list = ['a','b','c','d'] |
4 |
list.collect( newList ) { |
7 |
println newList // ["A", "B", "C", "D"] |
在上面的collect方法中,他接受一个一个List和闭包,上面的代码等价于下面的代码:
1 |
def list = ['a','b','c','d'] |
4 |
def clos = { it.toUpperCase() } |
5 |
list.collect( newList, clos ) |
7 |
assert newList == ["A", "B", "C", "D"] |
更多的信息:
Groovy继承了 java.lang.Object并且许多的 Collection 和Map 的许多方法都接受闭包作为参数的 See GDK Extensions to Object for practical uses of Groovy's Closures.
See Also:
- Java8-Function使用及Groovy闭包的代码示例
导航 定位 概述 代码示例 Java-Function Groovy闭包 定位 本文适用于想要了解Java8 Function接口编程及闭包表达式的筒鞋. 概述 在实际开发中,常常遇到使用模板模式的场 ...
- Groovy闭包详解
Groovy闭包是一种可执行代码块的方法,闭包也是对象,可以向方法一样传递参数,因为闭包也是对象,因此可以在需要的时候执行,像方法一样闭包可以传递一个或多个参数.闭包最常见的用途就是处理集合,可以遍历 ...
- 谈谈Groovy闭包
A closure is a function with variables bound to a context or environment in which it executes. 概述 闭包 ...
- Java8函数接口实现回调及Groovy闭包的代码示例
本文适用于想要了解Java8 Function接口编程及闭包表达式的筒鞋. 概述 在实际开发中,常常遇到使用模板模式的场景: 主体流程是不变的,变的只是其中要调用的具体方法. 其特征是: Begi ...
- lambda表达式和groovy闭包的区别
groovy定义的闭包是 Closure 的实例,lambda表达式只是在特定的接⼝或者抽象类的匿名实现,他们之间最主要区别闭包可以灵活的配置代理策略⽽labmda表达式不允许
- Groovy闭包
定义 闭包(Closure)是一种数据类型,它代表一段可执行的代码.它可以作为方法的参数,或者返回值,也可以独立运行,定义如下: def xxx = {parameters -> code} ...
- groovy闭包科里化参数
科里化闭包:带有预先绑定形参的闭包.在预先绑定一个形参之后,调用闭包时就不必为这个形参提供实参了.有助于去掉方法调用中的冗余重复. 使用curry方法科里化任意多个参数 使用rcurry方法科里化后面 ...
- Gradle学习之闭包
Gradle中的闭包其实就等同于Groovy中闭包,Groovy是一种jvm语言,语法兼容于java,曾几何时,也在脚本语言中独树一帜,初学Gradle的时候,大家很容易被其语法所迷惑,由于Gradl ...
- 基于Groovy+HttpRestful的超轻量级的接口测试用例配置的设计方案及DEMO实现
目标 设计一个轻量级测试用例框架,接口测试编写者只需要编写测试用例相关的内容(入参及结果校验),不需要理会系统的实现,不需要写跟测试校验无关的内容. 思路 测试用例分析 一个用例由以下部分组成: (1 ...
- 《Gradle权威指南》--Groovy基础
No1: Groovy中分号不是必须的 No2: Groovy中,单引号和双引号都可以定义一个字符串常量,不同的是单引号标记的是纯粹的字符串常量,而不是对字符串里的表达式做运算,但是双引号可以. ta ...
- Springmvc返回JSON乱码问号
@RequestMapping(value="/book/getBook.do", produces = "text/html;charset=UTF-8") ...
- JS判断数据是否是JSON类型
var isJson = function(obj){ var isjson = typeof(obj) == "object" && Object.pro ...
- 大话Git
Git是什么 Git是一个分布式版本控制系统.它可以很方便的记录你的每一次变动,而不需要每次都备份,还能让你和他人很方便的协同开发.这样你每次做了什么改动,瞄一眼就一清二楚了. -- 安装Git 从官 ...
- Android使用AttributeSet自定义控件的方法
所谓自定义控件(或称组件)也就是编写自己的控件类型,而非Android中提供的标准的控件,如TextView,CheckBox等等.不过自定义的控件一般也都是从标准控件继承来的,或者是多种控件组合,或 ...
- ASP.net MVC自定义错误处理页面的方法
在ASP.NET MVC中,我们可以使用HandleErrorAttribute特性来具体指定如何处理Action抛出的异常.只要某个Action设置了HandleErrorAttribute特性,那 ...
- [CareerCup] 4.7 Lowest Common Ancestor of a Binary Search Tree 二叉树的最小共同父节点
4.7 Design an algorithm and write code to find the first common ancestor of two nodes in a binary tr ...
- C#基础之垃圾回收
1.托管资源的回收 我们都知道C#托管资源的回收由GC全权负责控制,可是什么时候GC会回收垃圾呢?一般出现以下情况会回收垃圾:手动调用GC.Collect()强制回收:第0代对象内存已满:应用程序域被 ...
- 《TCP/IP详解卷1:协议》第11章 UDP:用户数据报协议-读书笔记
章节回顾: <TCP/IP详解卷1:协议>第1章 概述-读书笔记 <TCP/IP详解卷1:协议>第2章 链路层-读书笔记 <TCP/IP详解卷1:协议>第3章 IP ...
- angular的$scope,这东西满重要的
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 每天一个linux命令(43):lsof命令
lsof(list open files) 是一个列出当前系统打开文件的工具.在linux环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件.所以 如传输控制 ...