Groovy(三)基础
闭包
Groovy闭包很像Java中的匿名内部类
匿名内部内
闭包的使用方式
简单的定义
在Groovy中这种简单的匿名代码块就叫闭包
def closure = {println "hi"}
closure()
closure.call()
作为参数进行传递
def closure = {println("hi01")}
def fun_closure(closure){
closure();
}
fun_closure(closure);
fun_closure({println("hi02")})
fun_closure(){println("hi03")}
注意:当是构造器接收闭包作为参数时
闭包会与匿名内部类冲突
new class_closure({println("hi04")})
像闭包传递参数
默认一个参数时
def closure = {println("hi0$it")}
def fun_closure(closure){
closure("1");
}
fun_closure(closure);
多个参数时
def closure1 = { i1,i2->println("hi$i1$i2")}
def fun_closure1(closure){
closure("1","2");
}
fun_closure1(closure1);
|