【Groovy】循环控制 ( Number 注入函数实现循环 | times 函数 | upto 函数 | downto 函数 | step 函数 | 闭包作为最后参数可写在外面 )
生活随笔
收集整理的這篇文章主要介紹了
【Groovy】循环控制 ( Number 注入函数实现循环 | times 函数 | upto 函数 | downto 函数 | step 函数 | 闭包作为最后参数可写在外面 )
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 前言
- 一、times 循環函數
- 二、upto 循環函數
- 三、downto 循環函數
- 四、step 循環函數
- 1、step 循環函數遞增操作
- 2、step 循環函數遞減操作
- 五、閉包作為參數的使用規則
- 1、閉包作為最后一個參數可以寫到括號外面
- 2、函數參數括號可以省略、參數使用逗號隔開
- 六、完整代碼示例
前言
Groovy 為 Number 類實現的注入函數 , 也能實現循環 , 通過向注入的函數傳入閉包參數 , 即可實現循環操作 ;
一、times 循環函數
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 循環函數
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 循環函數
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 循環函數
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)1、step 循環函數遞增操作
代碼示例 :
// Groovy 向 Number 類中注入的 step 方法println ""print "( 10 ) : "10.step(30, 2, {// Integer it 就是每次的循環次數print it + " "})執行結果 :
( 10 ) : 10 12 14 16 18 20 22 24 26 282、step 循環函數遞減操作
代碼示例 :
// 遞減操作也可以println ""print "( 13 ) : "10.step(0, -2) {// Integer it 就是每次的循環次數print it + " "}執行結果 :
( 13 ) : 10 8 6 4 2五、閉包作為參數的使用規則
1、閉包作為最后一個參數可以寫到括號外面
代碼示例 :
// 如果調用函數時 , 函數參數最后一個元素是閉包 , 可以將閉包寫在外面println ""print "( 11 ) : "10.step(30, 2) {// Integer it 就是每次的循環次數print it + " "}執行結果 :
( 11 ) : 10 12 14 16 18 20 22 24 26 282、函數參數括號可以省略、參數使用逗號隔開
代碼示例 :
// 如果調用函數時 , 函數參數最后一個元素是閉包 , 可以將閉包寫在外面// 括號也可以去掉 , 但是三個參數之間需要使用逗號隔開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..9println ""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總結
以上是生活随笔為你收集整理的【Groovy】循环控制 ( Number 注入函数实现循环 | times 函数 | upto 函数 | downto 函数 | step 函数 | 闭包作为最后参数可写在外面 )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Groovy】循环控制 ( Java
- 下一篇: 【Groovy】集合声明与访问 ( 使用