Groovy 为 Number 类实现的注入函数 , 也能实现循环 , 通过向注入的函数传入闭包参数 , 即可实现循环操作 ;
Number 的注入函数 : 在 times 函数中 , 传入闭包 , 闭包中就是循环内容 ;
/**
* 从零开始多次执行闭包。每次都将当前索引传递给闭包。
* Example:
* <pre>10.times {
* println it
* }</pre>
* Prints the numbers 0 through 9.
*
* @param self a Number
* @param closure 闭包要调用多次
* @since 1.0
*/
public static void times(Number self, @ClosureParams(value=SimpleType.class,options="int") Closure closure)
代码示例 :
// 循环 10 次 , 每次获取获取当前循环的此处 , 取值 0 ~ 9
// Groovy 向 Number 类中注入的 times 方法
println ""
print "( 7 ) : "
10.times {
// Integer it 就是每次的循环次数
print it + " "
}
执行结果 :
( 7 ) : 0 1 2 3 4 5 6 7 8 9
upto 循环函数 : 传入一个大于 Number 的数值 , 自增循环 ;
/**
* 从该数字迭代到给定的数字(含),每次递增一。
*
* @param self a Number
* @param to another Number to go up to
* @param closure the closure to call
* @since 1.0
*/
public static void upto(Number self, Number to, @ClosureParams(FirstParam.class) Closure closure)
代码示例 :
// Groovy 向 Number 类中注入的 upto 方法
println ""
print "( 8 ) : "
10.upto(20, {
// Integer it 就是每次的循环次数
print it + " "
})
执行结果 :
( 8 ) : 10 11 12 13 14 15 16 17 18 19 20
downto 循环函数 : 传入一个小于 Number 的数值 , 自减循环 ;
/**
* 从这个数字迭代到给定的数字,每次递减一。
*
* @param self a Number
* @param to another Number to go down to
* @param closure the closure to call
* @since 1.0
*/
public static void downto(Number self, Number to, @ClosureParams(FirstParam.class) Closure closure)
代码示例 :
// Groovy 向 Number 类中注入的 downto 方法
println ""
print "( 9 ) : "
20.downto(10, {
// Integer it 就是每次的循环次数
print it + " "
})
执行结果 :
( 9 ) : 20 19 18 17 16 15 14 13 12 11 10
step 循环函数 : 传入一个值 to , 以 stepNumber 步长进行迭代 ;
/**
* 使用步长增量从该数字迭代到给定数字。每个中间编号都传递给给定的闭包。例子:
* <pre>0.step( 10, 2 ) {
* println it
* }</pre>
* Prints even numbers 0 through 8.
*
* @param self a Number to start with
* @param to a Number to go up to, exclusive
* @param stepNumber a Number representing the step increment
* @param closure the closure to call
* @since 1.0
*/
public static void step(Number self, Number to, Number stepNumber, Closure closure)
代码示例 :
// Groovy 向 Number 类中注入的 step 方法
println ""
print "( 10 ) : "
10.step(30, 2, {
// Integer it 就是每次的循环次数
print it + " "
})
执行结果 :
( 10 ) : 10 12 14 16 18 20 22 24 26 28
代码示例 :
// 递减操作也可以
println ""
print "( 13 ) : "
10.step(0, -2) {
// Integer it 就是每次的循环次数
print it + " "
}
执行结果 :
( 13 ) : 10 8 6 4 2
代码示例 :
// 如果调用函数时 , 函数参数最后一个元素是闭包 , 可以将闭包写在外面
println ""
print "( 11 ) : "
10.step(30, 2) {
// Integer it 就是每次的循环次数
print it + " "
}
执行结果 :
( 11 ) : 10 12 14 16 18 20 22 24 26 28
代码示例 :
// 如果调用函数时 , 函数参数最后一个元素是闭包 , 可以将闭包写在外面
// 括号也可以去掉 , 但是三个参数之间需要使用逗号隔开
println ""
print "( 12 ) : "
10.step 30, 2, {
// Integer it 就是每次的循环次数
print it + " "
}
执行结果 :
( 12 ) : 10 12 14 16 18 20 22 24 26 28
class Test {
static void main(args) {
// Java 语法样式的循环
println ""
print "( 0 ) : "
for (int j = 0; j <= 9; j++) {
print j + " "
}
// Groovy 循环 , 0 ~ 9 进行循环
println ""
print "( 1 ) : "
for (i in new IntRange(0, 9)) {
print i + " "
}
// Groovy 循环 , 0 ~ 9 进行循环
println ""
print "( 2 ) : "
for (i in new IntRange(0, 9, false)) {
print i + " "
}
// Groovy 循环 , 9 ~ 0 进行循环
println ""
print "( 3 ) : "
for (i in new IntRange(0, 9, true)) {
print i + " "
}
// Groovy 循环 , 0 ~ 9 进行循环 , 不包含最后一个 to 元素 , 即 9
// 只能打印出 0 ~ 8 的数字
println ""
print "( 4 ) : "
for (i in new IntRange(false, 0, 9)) {
print i + " "
}
// Groovy 循环 , 0 ~ 9 进行循环 , 包含最后一个 to 元素 , 即 9
// 只能打印出 0 ~ 9 的数字
println ""
print "( 5 ) : "
for (i in new IntRange(true, 0, 9)) {
print i + " "
}
// Groovy 循环 , 0 ~ 9 进行循环
println ""
print "( 6 ) : "
for (i in 0..9) {
print i + " "
}
// 其中 0..9 相当于 new IntRange(0, 9)
def range = 0..9
println ""
print "0..9 type : "
println range.class
// 循环 10 次 , 每次获取获取当前循环的此处 , 取值 0 ~ 9
// Groovy 向 Number 类中注入的 times 方法
println ""
print "( 7 ) : "
10.times {
// Integer it 就是每次的循环次数
print it + " "
}
// Groovy 向 Number 类中注入的 upto 方法
println ""
print "( 8 ) : "
10.upto(20, {
// Integer it 就是每次的循环次数
print it + " "
})
// Groovy 向 Number 类中注入的 downto 方法
println ""
print "( 9 ) : "
20.downto(10, {
// Integer it 就是每次的循环次数
print it + " "
})
// Groovy 向 Number 类中注入的 downto 方法
println ""
print "( 9 ) : "
20.downto(10, {
// Integer it 就是每次的循环次数
print it + " "
})
// Groovy 向 Number 类中注入的 step 方法
println ""
print "( 10 ) : "
10.step(30, 2, {
// Integer it 就是每次的循环次数
print it + " "
})
// 如果调用函数时 , 函数参数最后一个元素是闭包 , 可以将闭包写在外面
println ""
print "( 11 ) : "
10.step(30, 2) {
// Integer it 就是每次的循环次数
print it + " "
}
// 如果调用函数时 , 函数参数最后一个元素是闭包 , 可以将闭包写在外面
// 括号也可以去掉 , 但是三个参数之间需要使用逗号隔开
println ""
print "( 12 ) : "
10.step 30, 2, {
// Integer it 就是每次的循环次数
print it + " "
}
// 递减操作也可以
println ""
print "( 13 ) : "
10.step(0, -2) {
// Integer it 就是每次的循环次数
print it + " "
}
println ""
}
}
执行结果 :
( 0 ) : 0 1 2 3 4 5 6 7 8 9
( 1 ) : 0 1 2 3 4 5 6 7 8 9
( 2 ) : 0 1 2 3 4 5 6 7 8 9
( 3 ) : 9 8 7 6 5 4 3 2 1 0
( 4 ) : 0 1 2 3 4 5 6 7 8
( 5 ) : 0 1 2 3 4 5 6 7 8 9
( 6 ) : 0 1 2 3 4 5 6 7 8 9
0..9 type : class groovy.lang.IntRange
( 7 ) : 0 1 2 3 4 5 6 7 8 9
( 8 ) : 10 11 12 13 14 15 16 17 18 19 20
( 9 ) : 20 19 18 17 16 15 14 13 12 11 10
( 9 ) : 20 19 18 17 16 15 14 13 12 11 10
( 10 ) : 10 12 14 16 18 20 22 24 26 28
( 11 ) : 10 12 14 16 18 20 22 24 26 28
( 12 ) : 10 12 14 16 18 20 22 24 26 28
( 13 ) : 10 8 6 4 2