Groovy入门

来源:互联网 发布:麻瓜编程爬虫 编辑:程序博客网 时间:2024/06/05 06:20
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"}

原创粉丝点击