Gradle基础知识——Groovy的闭包
2017-10-17 22:51
731 查看
定义闭包
def closure_name = { // closure body }
上面代码定义一个名为 closure_name 的闭包,用途由 closure body 中的代码定义。匿名闭包指不声明闭包变量名,只有闭包方法体
{ //closure body }
无参闭包
def closure_with_no_param = { println 'hello,world!' }
执行
closure_with_no_param()或者
closure_with_no_param.call(),将输出
hello,world!
含参闭包
def closure_with_param = { x,y-> println "x plus y is " + (x+y) }
执行
closure_with_param(1,2),结果为
x plus y is 3!
可以设置默认参数值,例如:
def closure_with_param = { x,y=0-> println "x plus y is " + (x+y) }
执行
closure_with_param(1),结果为
x plus y is 1!
与方法/函数的结合使用
定义闭包
def closure_demo = { x -> println x }
定义方法
def method_name(Closure closure_name){ for(int i=0;i<=100;i+=1){ closure_name(i) } }
执行
method_name(closure_demo)或者
method_name closure_demo,结果输出如下:
1 2 3 ... 100
Gradle构建脚本简析
dependencies { compile group: 'commons-collections', name: 'commons-collections', version: '3.2' testCompile group: 'junit', name: 'junit', version: '4.+' }
其中:
dependencies为方法或函数名,参数为闭包类型
接下来的
{...}是一个闭包
//这是个闭包 { compile group: 'commons-collections', name: 'commons-collections', version: '3.2' testCompile group: 'junit', name: 'junit', version: '4.+' }
group: 'commons-collections'将
group变量的值设为
commons-collections
compile为方法/函数
参考文献
https://www.w3cschool.cn/groovy/groovy_closures.html
http://blog.csdn.net/cckevincyh/article/details/75212415
相关文章推荐
- gradle使用的groovy基础知识
- Android studio:Groovy 与 Gradle 基础【三】
- Groovy基础——Closure(闭包)详解
- gradle学习(12)-groovy一些基础语法
- 【二】Gradle 基础知识学习笔记
- Gradle从入门到实战 - Groovy基础
- javascript基础知识复习1-关于闭包
- 【三】Gradle 基础知识学习笔记
- Groovy 使 Spring 更出色,第 1 部分: 集成的基础知识
- gradle 知识(2) -- groovy语法
- Gradle详解-脚本基础知识
- 基于JVM的动态语言Groovy 基础知识汇总
- Gradle系列之Groovy基础
- Gradle基础之Groovy语法
- Groovy 和 Gradle (Android Studio)基础
- android gradle 基础知识(一)
- Gradle学习基础(3):build脚本基础知识
- Gradle基础知识
- Gradle从入门到实战 - Groovy基础
- Gradle从入门到实战 - Groovy基础