本文共 523 字,大约阅读时间需要 1 分钟。
def list = [1, 2, 3]list.each { e -> print e}println()list << 4list.each { print it //默认参数}println()//定义闭包def add = { a, b -> println "a + b = ${a + b}"}def minus = { a, b -> println "a - b = ${a - b}"}//调用闭包add 1, 2minus 1, 1//定义函数,参数类型为闭包def myfunc(a, b, Closure closure){ closure a, b}//调用函数myfunc 2, 2, { a, b -> println "a * b = ${a * b}"}//调用函数myfunc 1, 1, minus//定义函数,只有一个参数,参数类型是闭包def show(Closure closure){ closure.call()}//调用函数,可以省略(),如果最后一个参数的类型是闭包,可以提取出来show { println "hello the world"}