日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

【Groovy】循环控制 ( Number 注入函数实现循环 | times 函数 | upto 函数 | downto 函数 | step 函数 | 闭包作为最后参数可写在外面 )

發(fā)布時(shí)間:2025/6/17 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Groovy】循环控制 ( Number 注入函数实现循环 | times 函数 | upto 函数 | downto 函数 | step 函数 | 闭包作为最后参数可写在外面 ) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

  • 前言
  • 一、times 循環(huán)函數(shù)
  • 二、upto 循環(huán)函數(shù)
  • 三、downto 循環(huán)函數(shù)
  • 四、step 循環(huán)函數(shù)
    • 1、step 循環(huán)函數(shù)遞增操作
    • 2、step 循環(huán)函數(shù)遞減操作
  • 五、閉包作為參數(shù)的使用規(guī)則
    • 1、閉包作為最后一個(gè)參數(shù)可以寫到括號(hào)外面
    • 2、函數(shù)參數(shù)括號(hào)可以省略、參數(shù)使用逗號(hào)隔開
  • 六、完整代碼示例

前言


Groovy 為 Number 類實(shí)現(xiàn)的注入函數(shù) , 也能實(shí)現(xiàn)循環(huán) , 通過(guò)向注入的函數(shù)傳入閉包參數(shù) , 即可實(shí)現(xiàn)循環(huán)操作 ;





一、times 循環(huán)函數(shù)



Number 的注入函數(shù) : 在 times 函數(shù)中 , 傳入閉包 , 閉包中就是循環(huán)內(nèi)容 ;

/*** 從零開始多次執(zhí)行閉包。每次都將當(dāng)前索引傳遞給閉包。* Example:* <pre>10.times {* println it* }</pre>* Prints the numbers 0 through 9.** @param self a Number* @param closure 閉包要調(diào)用多次* @since 1.0*/public static void times(Number self, @ClosureParams(value=SimpleType.class,options="int") Closure closure)

代碼示例 :

// 循環(huán) 10 次 , 每次獲取獲取當(dāng)前循環(huán)的此處 , 取值 0 ~ 9// Groovy 向 Number 類中注入的 times 方法println ""print "( 7 ) : "10.times {// Integer it 就是每次的循環(huán)次數(shù)print it + " "}

執(zhí)行結(jié)果 :

( 7 ) : 0 1 2 3 4 5 6 7 8 9



二、upto 循環(huán)函數(shù)



upto 循環(huán)函數(shù) : 傳入一個(gè)大于 Number 的數(shù)值 , 自增循環(huán) ;

/*** 從該數(shù)字迭代到給定的數(shù)字(含),每次遞增一。** @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 就是每次的循環(huán)次數(shù)print it + " "})

執(zhí)行結(jié)果 :

( 8 ) : 10 11 12 13 14 15 16 17 18 19 20



三、downto 循環(huán)函數(shù)



downto 循環(huán)函數(shù) : 傳入一個(gè)小于 Number 的數(shù)值 , 自減循環(huán) ;

/*** 從這個(gè)數(shù)字迭代到給定的數(shù)字,每次遞減一。** @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 就是每次的循環(huán)次數(shù)print it + " "})

執(zhí)行結(jié)果 :

( 9 ) : 20 19 18 17 16 15 14 13 12 11 10



四、step 循環(huán)函數(shù)



step 循環(huán)函數(shù) : 傳入一個(gè)值 to , 以 stepNumber 步長(zhǎng)進(jìn)行迭代 ;

/*** 使用步長(zhǎng)增量從該數(shù)字迭代到給定數(shù)字。每個(gè)中間編號(hào)都傳遞給給定的閉包。例子:* <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 循環(huán)函數(shù)遞增操作


代碼示例 :

// Groovy 向 Number 類中注入的 step 方法println ""print "( 10 ) : "10.step(30, 2, {// Integer it 就是每次的循環(huán)次數(shù)print it + " "})

執(zhí)行結(jié)果 :

( 10 ) : 10 12 14 16 18 20 22 24 26 28

2、step 循環(huán)函數(shù)遞減操作


代碼示例 :

// 遞減操作也可以println ""print "( 13 ) : "10.step(0, -2) {// Integer it 就是每次的循環(huán)次數(shù)print it + " "}

執(zhí)行結(jié)果 :

( 13 ) : 10 8 6 4 2



五、閉包作為參數(shù)的使用規(guī)則




1、閉包作為最后一個(gè)參數(shù)可以寫到括號(hào)外面


代碼示例 :

// 如果調(diào)用函數(shù)時(shí) , 函數(shù)參數(shù)最后一個(gè)元素是閉包 , 可以將閉包寫在外面println ""print "( 11 ) : "10.step(30, 2) {// Integer it 就是每次的循環(huán)次數(shù)print it + " "}

執(zhí)行結(jié)果 :

( 11 ) : 10 12 14 16 18 20 22 24 26 28

2、函數(shù)參數(shù)括號(hào)可以省略、參數(shù)使用逗號(hào)隔開


代碼示例 :

// 如果調(diào)用函數(shù)時(shí) , 函數(shù)參數(shù)最后一個(gè)元素是閉包 , 可以將閉包寫在外面// 括號(hào)也可以去掉 , 但是三個(gè)參數(shù)之間需要使用逗號(hào)隔開println ""print "( 12 ) : "10.step 30, 2, {// Integer it 就是每次的循環(huán)次數(shù)print it + " "}

執(zhí)行結(jié)果 :

( 12 ) : 10 12 14 16 18 20 22 24 26 28



六、完整代碼示例



class Test {static void main(args) {// Java 語(yǔ)法樣式的循環(huán)println ""print "( 0 ) : "for (int j = 0; j <= 9; j++) {print j + " "}// Groovy 循環(huán) , 0 ~ 9 進(jìn)行循環(huán)println ""print "( 1 ) : "for (i in new IntRange(0, 9)) {print i + " "}// Groovy 循環(huán) , 0 ~ 9 進(jìn)行循環(huán)println ""print "( 2 ) : "for (i in new IntRange(0, 9, false)) {print i + " "}// Groovy 循環(huán) , 9 ~ 0 進(jìn)行循環(huán)println ""print "( 3 ) : "for (i in new IntRange(0, 9, true)) {print i + " "}// Groovy 循環(huán) , 0 ~ 9 進(jìn)行循環(huán) , 不包含最后一個(gè) to 元素 , 即 9// 只能打印出 0 ~ 8 的數(shù)字println ""print "( 4 ) : "for (i in new IntRange(false, 0, 9)) {print i + " "}// Groovy 循環(huán) , 0 ~ 9 進(jìn)行循環(huán) , 包含最后一個(gè) to 元素 , 即 9// 只能打印出 0 ~ 9 的數(shù)字println ""print "( 5 ) : "for (i in new IntRange(true, 0, 9)) {print i + " "}// Groovy 循環(huán) , 0 ~ 9 進(jìn)行循環(huán)println ""print "( 6 ) : "for (i in 0..9) {print i + " "}// 其中 0..9 相當(dāng)于 new IntRange(0, 9)def range = 0..9println ""print "0..9 type : "println range.class// 循環(huán) 10 次 , 每次獲取獲取當(dāng)前循環(huán)的此處 , 取值 0 ~ 9// Groovy 向 Number 類中注入的 times 方法println ""print "( 7 ) : "10.times {// Integer it 就是每次的循環(huán)次數(shù)print it + " "}// Groovy 向 Number 類中注入的 upto 方法println ""print "( 8 ) : "10.upto(20, {// Integer it 就是每次的循環(huán)次數(shù)print it + " "})// Groovy 向 Number 類中注入的 downto 方法println ""print "( 9 ) : "20.downto(10, {// Integer it 就是每次的循環(huán)次數(shù)print it + " "})// Groovy 向 Number 類中注入的 downto 方法println ""print "( 9 ) : "20.downto(10, {// Integer it 就是每次的循環(huán)次數(shù)print it + " "})// Groovy 向 Number 類中注入的 step 方法println ""print "( 10 ) : "10.step(30, 2, {// Integer it 就是每次的循環(huán)次數(shù)print it + " "})// 如果調(diào)用函數(shù)時(shí) , 函數(shù)參數(shù)最后一個(gè)元素是閉包 , 可以將閉包寫在外面println ""print "( 11 ) : "10.step(30, 2) {// Integer it 就是每次的循環(huán)次數(shù)print it + " "}// 如果調(diào)用函數(shù)時(shí) , 函數(shù)參數(shù)最后一個(gè)元素是閉包 , 可以將閉包寫在外面// 括號(hào)也可以去掉 , 但是三個(gè)參數(shù)之間需要使用逗號(hào)隔開println ""print "( 12 ) : "10.step 30, 2, {// Integer it 就是每次的循環(huán)次數(shù)print it + " "}// 遞減操作也可以println ""print "( 13 ) : "10.step(0, -2) {// Integer it 就是每次的循環(huán)次數(shù)print it + " "}println ""} }

執(zhí)行結(jié)果 :

( 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

總結(jié)

以上是生活随笔為你收集整理的【Groovy】循环控制 ( Number 注入函数实现循环 | times 函数 | upto 函数 | downto 函数 | step 函数 | 闭包作为最后参数可写在外面 )的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。