前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Groovy】循环控制 ( Number 注入函数实现循环 | times 函数 | upto 函数 | downto 函数 | step 函数 | 闭包作为最后参数可写在外面 )

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

作者头像
韩曙亮
发布2023-03-30 09:43:06
4780
发布2023-03-30 09:43:06
举报
文章被收录于专栏:韩曙亮的移动开发专栏

文章目录

前言

Groovy 为 Number 类实现的注入函数 , 也能实现循环 , 通过向注入的函数传入闭包参数 , 即可实现循环操作 ;

一、times 循环函数


Number 的注入函数 : 在 times 函数中 , 传入闭包 , 闭包中就是循环内容 ;

代码语言:javascript
复制
    /**
     * 从零开始多次执行闭包。每次都将当前索引传递给闭包。
     * 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) 

代码示例 :

代码语言:javascript
复制
        // 循环 10 次 , 每次获取获取当前循环的此处 , 取值 0 ~ 9
        // Groovy 向 Number 类中注入的 times 方法
        println ""
        print "( 7 ) : "
        10.times {
            // Integer it 就是每次的循环次数
            print it + " "
        }

执行结果 :

代码语言:javascript
复制
( 7 ) : 0 1 2 3 4 5 6 7 8 9 

二、upto 循环函数


upto 循环函数 : 传入一个大于 Number 的数值 , 自增循环 ;

代码语言:javascript
复制
    /**
     * 从该数字迭代到给定的数字(含),每次递增一。
     *
     * @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) 

代码示例 :

代码语言:javascript
复制
        // Groovy 向 Number 类中注入的 upto 方法
        println ""
        print "( 8 ) : "
        10.upto(20, {
            // Integer it 就是每次的循环次数
            print it + " "
        })

执行结果 :

代码语言:javascript
复制
( 8 ) : 10 11 12 13 14 15 16 17 18 19 20 

三、downto 循环函数


downto 循环函数 : 传入一个小于 Number 的数值 , 自减循环 ;

代码语言:javascript
复制
    /**
     * 从这个数字迭代到给定的数字,每次递减一。
     *
     * @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) 

代码示例 :

代码语言:javascript
复制
        // Groovy 向 Number 类中注入的 downto 方法
        println ""
        print "( 9 ) : "
        20.downto(10, {
            // Integer it 就是每次的循环次数
            print it + " "
        })

执行结果 :

代码语言:javascript
复制
( 9 ) : 20 19 18 17 16 15 14 13 12 11 10 

四、step 循环函数


step 循环函数 : 传入一个值 to , 以 stepNumber 步长进行迭代 ;

代码语言:javascript
复制
    /**
     * 使用步长增量从该数字迭代到给定数字。每个中间编号都传递给给定的闭包。例子:
     * <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 循环函数递增操作

代码示例 :

代码语言:javascript
复制
        // Groovy 向 Number 类中注入的 step 方法
        println ""
        print "( 10 ) : "
        10.step(30, 2, {
            // Integer it 就是每次的循环次数
            print it + " "
        })

执行结果 :

代码语言:javascript
复制
( 10 ) : 10 12 14 16 18 20 22 24 26 28 

2、step 循环函数递减操作

代码示例 :

代码语言:javascript
复制
        // 递减操作也可以
        println ""
        print "( 13 ) : "
        10.step(0, -2) {
            // Integer it 就是每次的循环次数
            print it + " "
        }

执行结果 :

代码语言:javascript
复制
( 13 ) : 10 8 6 4 2 

五、闭包作为参数的使用规则


1、闭包作为最后一个参数可以写到括号外面

代码示例 :

代码语言:javascript
复制
        // 如果调用函数时 , 函数参数最后一个元素是闭包 , 可以将闭包写在外面
        println ""
        print "( 11 ) : "
        10.step(30, 2) {
            // Integer it 就是每次的循环次数
            print it + " "
        }

执行结果 :

代码语言:javascript
复制
( 11 ) : 10 12 14 16 18 20 22 24 26 28 

2、函数参数括号可以省略、参数使用逗号隔开

代码示例 :

代码语言:javascript
复制
        // 如果调用函数时 , 函数参数最后一个元素是闭包 , 可以将闭包写在外面
        // 括号也可以去掉 , 但是三个参数之间需要使用逗号隔开
        println ""
        print "( 12 ) : "
        10.step 30, 2, {
            // Integer it 就是每次的循环次数
            print it + " "
        }

执行结果 :

代码语言:javascript
复制
( 12 ) : 10 12 14 16 18 20 22 24 26 28 

六、完整代码示例


代码语言:javascript
复制
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 ""
    }
}

执行结果 :

代码语言:javascript
复制
( 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 
在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-12-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 前言
  • 一、times 循环函数
  • 二、upto 循环函数
  • 三、downto 循环函数
  • 四、step 循环函数
    • 1、step 循环函数递增操作
      • 2、step 循环函数递减操作
      • 五、闭包作为参数的使用规则
        • 1、闭包作为最后一个参数可以写到括号外面
          • 2、函数参数括号可以省略、参数使用逗号隔开
          • 六、完整代码示例
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档