文章目录

  • 前言
  • 一、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 28

2、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 28

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

代码示例 :

        // 如果调用函数时 , 函数参数最后一个元素是闭包 , 可以将闭包写在外面// 括号也可以去掉 , 但是三个参数之间需要使用逗号隔开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 函数 | 闭包作为最后参数可写在外面 )相关推荐

  1. Simulink代码生成:Step函数接口配置

    本文研究Simulink生成代码时的step函数的名称和参数. 文章目录 1 问题引入 2 配置过程 3 代码生成 4 总结 1 问题引入 在之前的一篇博客<Simulink代码生成:Simul ...

  2. 【ADAMS学习记录】——STEP函数添加运动驱动

    STEP 函数为运动副添加驱动 step函数格式和定义 STEP(x,x0,h0,x1,h1) x:自变量,可以是时间或时间的任一函数 x0 :自变量的STEP函数开始值,可以是常数.函数表达式或设计 ...

  3. nodejs-REPL/回调函数/事件循环

    REPL 回调函数 事件循环REPL----------------------------------------------------- Node.js REPL(Read Eval Print ...

  4. 返回函数之循环变量问题

    返回函数之循环变量问题 一.学习要点: 1.返回函数创建的时候不被执行,调用的时候才被执行: 2.返回函数不要应用任何循环变量或者后续会发生变化的变量: 3.如果一定要引用循环变量,方法是在创建一个函 ...

  5. [转载] Python中定义函数,循环语句,条件语句

    参考链接: Python中的局部函数 由于日常程序流中主要是三种结构:顺序,循环,条件,且往往需要自定义函数再调用, 因此今天想学习一下Python中关于定义函数.循环语句和条件语句的写法. 1.定义 ...

  6. Smarty - 手册 - 第8章 自定义函数 - {cycle}循环

    Smarty - 手册 - 第8章 自定义函数 - {cycle}循环 {cycle} {cycle} is used to alternate a set of values. This makes ...

  7. ThinkPHP6 模板引擎普通标签中,模板引擎运算符函数,循环标签,判断标签的使用,及一些特殊标签

    ThinkPHP6 模板引擎普通标签中,模板引擎运算符函数,循环标签,判断标签的使用,及一些特殊标签 模板引擎支持普通标签和XML标签方式两种标签定义,分别用于不同的目的: 标签类型 描述 普通标签 ...

  8. matlab循环中调用函数,MATLAB循环和函数定义,调用

    格式不要括号,最后有end for 循环变量 = 表达式1:表 2:表 3 表1:初值     表2:步长      表3:终值 求圆周率:π/4=1 - 1/3 + 1/5 -1/7+...+(-1 ...

  9. Rust——猜数游戏、数据类型、函数、循环和if条件表达式

    目录 一.数据类型 (一).基础类型 (二).进制 (三).复合类型 ①tuple ②数组 二.函数 三.循环和if (一).if (二).loop (三).while (四).for 四.猜数游戏 ...

最新文章

  1. GPU — CUDA 编程模型
  2. Web 趋势榜:上周最有意思又热门的 10 大 Web 项目 - 210709
  3. cocos2dx--cocos2dx3.1.1执行报无法解析的外部符号
  4. 为myeclipse分配更大的内存
  5. 电脑怎么重置host_电脑又双叒叕卡顿?究竟要“重装”还是“重置”?原来这区别大了...
  6. 兴利调节matlab编程,水利计算之兴利调节计算.ppt
  7. Qt与Visual Assitst X的集成问题
  8. OpenCV(C++版)图像读取,创建,复制,保存,显示
  9. 在C#中如何读取枚举值的描述属性
  10. hadoop 学习
  11. 遥控器进入鼠标模式,退出鼠标模式,上下左右移动和确认
  12. tomcat里面的war包解压后没有访问权限
  13. python列表所有元素平均值_python—组合数据类型
  14. 树莓派linux下载机,树莓派打造北邮人种子下载机——下载、做种一条龙全站式教程...
  15. (一)数据分析——企业的贤内助(数据分析的价值)
  16. codeforces 1520E. Arranging The Sheep(1400)
  17. 经典智力题:火车运煤
  18. 小米盒子4S Pro好不好?对比当贝盒子B1值得买吗?
  19. 解决idea集成maven在使用骨架构建项目报错问题
  20. Esri合作伙伴Track Star集成ArcGIS平台

热门文章

  1. 致青春VS杜蕾斯,用QQ空间电影大数据解读关联性
  2. 查看数据库表使用空间大小
  3. 常用的 服务器 与 交换机
  4. VS2008 Tips #004 – 您可以通过“浏览方式…”添加浏览器到 Visual Web Developer
  5. 使用php与mysql构建我们的网站
  6. laravel 同数据表字段比较查询和状态不正规排序
  7. JSON返回DateTime/Date('123123123')/解决办法
  8. 四周第四次课(1月5日) 6.1 压缩打包介绍 6.2 gzip压缩工具 6.3 bzip2压缩工具 6.4 xz压缩工具...
  9. Python学习之路-12 (递归)
  10. 「学习笔记」多项式相关