什么是闭包
一个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 ...
- sublime配置全攻略
大家好,今天给大家分享一款编辑器:sublime text2 我用过很多编辑器, EditPlus.EmEditor.Notepad++.Notepad2.UltraEdit.Editra.V ...
- MVC权限验证之ActionFilterAttribute
参考:http://www.cnblogs.com/waitingfor/archive/2011/12/27/2303784.html ActionFilterAttribute是Action过滤类 ...
- 1017. A除以B (20)
本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数.你需要输出商数Q和余数R,使得A = B * Q + R成立. 输入格式: 输入在1行中依次给出A和B,中间以1空格分隔. 输出格 ...
- freemarker语法简介
ftl是一种模板标记语言,用于渲染数据,输入html结构.语法简介如下: ${book.name} ${book.name?if_exists} //值是否存在 ${book.name??} //值是 ...
- C#脚本引擎 CS-Script 之(三)——如何部署
本文不但介绍了CS-Script如何部署,还介绍了CS-Script的部署后面的原理,并用一个框图详细介绍了部署中的各种细节. 一.获取资源 1.从官网上下载编译好的csscript资源:cs-scr ...
- [CareerCup] 8.7 Chat Server 聊天服务器
8.7 Explain how you would design a chat server. In particular, provide details about the various bac ...
- 进程&信号&管道实践学习记录
程序分析 exec1.c & exect2.c & exect3.c 程序代码 (以exect1.c为例,其他两个结构类似) #include <stdio.h> #inc ...
- iOS中UIMenuController的使用
不知你有没有发现,在你的微信朋友中,长按一段文字(正文或者评论),会弹出这样子的玩意: 要想在你的view或者viewController中实现长按弹出菜单栏你必须要调用becomeFirstResp ...
- polya计数定理在ACM-icpc中的应用
[数学公式] PG(x1,x2,...,xn) = 1/|G| * ∑π∈G x1^b1 * x2^b2*...*bn^bn 其中π是1^b12^b2...n^bn型轮换 然后一般染色情况下x1= ...
- JS实现星级评价
说明: 本方法采用了Jquery库,暂时检测兼容IE8版本.本示例的2种颜色的星星都是放入了一张png图片当中,当然还有其他的一些实现思路.本示例展示的情况是当前页面只有一个星级评价的情况. 思路: ...