天天看点

groovy 闭包基础 1. groovy中闭包基础知识2. Groovy闭包的使用

1. groovy中闭包基础知识

闭包其实就是一段代码块,在使用时和方法很类似,可是与方法有较大的差别。

主要包括闭包概念,闭包参数以及闭包返回值。

1.1 闭包的定义和调用

简单闭包定义:

def clouser = {println 'hello groovy'}
clouser.call()
//clouser()
           

输出结果:

hello groovy
           

1.2 闭包参数

定义有参数的闭包,需要使用->,在->之前是参数,->之后是闭包体。

def clouser = {String name ->println "hello ${name}"}
//clouser.call('groovy')
clouser('groovy')
           

输出结果:

hello groovy
           

如果有多个参数,使用逗号隔开。

def clouser = {String name, int age ->
    println "hello ${name}, age is ${age}"}
clouser('groovy', 4)
           

输出结果:

hello groovy, age is 4
           

任意闭包有一个默认参数,不需要显示申明,用it表示。

def clouser = {
    println "hello ${it}"}
clouser('groovy')
           

输出结果:

hello groovy
           

如果不想闭包中有it这个隐式参数,只需要定义一个显示参数,这样闭包就没有it这个隐式参数了。

1.3 闭包返回值

正常流程下,使用return返回

def clouser = {
    return  "hello ${it}"
}
def result = clouser('groovy')
println result;
           

输出结果:

hello groovy
           

闭包一定会有返回值,默认返回最后一句代码的结果

def clouser = {
    println "hello ${it}"
}
def result = clouser('groovy')
println result
           

输出结果:

hello groovy
null
           

2. Groovy闭包的使用

闭包用途非常多,这里列举闭包常用的4个方面:

2.1 与基本类型的结合使用

Groovy增加了upto和downto函数来进行循环。 

int x = fab(5)
println x
int y = fab(5)
print y

int fab(int number) {
    int result = 1
    1.upto(number, { num -> result *= num})
    return result
}

int fab2(int number) {
    int result = 1
    number.downto(1) {
        num -> result *= num
    }
    return result
}
           

输出结果:

120
120
           

m.upto(n) 方法是从m开始循环到n,步长是1,循环n-m次。

m.downto(n) 方法是从m开始循环到n,步长是-1,循环m-n次。

如果闭包作为方法参数最后一个,可以写在括号体内,也可以写在括号体外,两种写法效果一样。

还有一个常用的方法n.times()方法, 表示循环n次,循环变量it从0开始到n结束

int x = cal(5)
println x

int cal(int number) {
    int result = 0
    number.times {
        num ->
            result += num
    }
    return  result
}
           

输出结果:

10
           

还有其他与基本类型使用的方法,具体可以从DefaultGroovyMethods.java中查看,闭包参数可以从源码查看,具体查看它调用闭包的时候有没有传入参数,定义的闭包需要匹配方法传入的参数。

3.2 与String结合使用

Groovy字符串提供了each()方法来进行遍历,返回值是字符串本身。代码:

String str = 'the 2 and 3 is 5'
str.each {
    String temp -> print temp
}
           

输出结果:

the 2 and 3 is 5
           

通过find()方法进行查找,find方法闭包必须返回一个布尔类型的值,满足条件时将会作为find方法的返回,它只查找第一个满足条件的值。

String str = 'the 2 and 3 is 5'
println str.find {
    String s -> s.isNumber()
}
           

输出结果:

2
           

findAll()方法,查找所有满足条件的字符,用法和find()方法一样

String str = 'the 2 and 3 is 5'
def list = str.findAll {
    String s -> s.isNumber()
}
println list.toListString()
           

输出结果:

[2, 3, 5]
           

经常使用的还有any()和every()方法,any()方法表示字符串是否含有满足条件的字符。

String str = 'the 2 and 3 is 5'
def result = str.any {
    String s -> s.isNumber()
}
println result
           

输出结果:

true
           

表示字符串里面还有数字字符。

every()方法表示字符串中是否每个字符都满足条件,如果是,返回true,否则返回false。

String str = 'the 2 and 3 is 5'
def result = str.every {
    String s -> s.isNumber()
}
println result
           

输出结果:

false
           

collect()方法,表示将字符串每个元素都应用于闭包,闭包会产生一个新的结果,然后将结果添加到List中进行返回。

String str = 'the 2 and 3 is 5'
def list = str.collect {
    it.toUpperCase()
}
println list
           

输出结果:

[T, H, E,  , 2,  , A, N, D,  , 3,  , I, S,  , 5]
           

继续阅读