groovy–流程控制
在本篇文章中,我们将介绍逻辑分支,循环,以及如何从if-else以及try-catch代码块中返回值。 if – else Groovy 支持Java传统的if-else语法: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 def x = false def y = false if ( !x ) { x = true } assert x == true if ( x ) { x = false } else { y = true } assert x == y Groovy 也支持Java传统的if-else if -else 语法: 1 if( ... ) { 2 ... 3 }elseif(...) { 4 ... 5 }else{ 6 ... 7 } 三元操作符 Groovy 也支持Java传统的三元操作符: 1 defy =5 2 defx = (y >1) ?"worked":"failed" 3 assertx =="worked" switch Groovy也支持switch语句,不过和java的switch语句还是有很大的区别的: 1 defx =1.23 2 defresult ="" 3 4 switch( x ) { 5 case"foo": 6 result ="found foo" 7 // lets fall through 8 9 case"bar": 10 result +="bar" 11 12 case[4,5,6,'inList']: 13 result ="list" 14 break 15 16 case12..30: 17 result ="range" 18 break 19 20 caseInteger: 21 result ="integer" 22 break 23 24 caseNumber: 25 result ="number" 26 break 27 28 default: 29 result ="default" 30 } 31 32 assertresult =="number" 从上面的例子可以看出switch ( x )中的x可以使用任何类型的值,而且下面的匹配值也可以使用任何的类型。 循环 Groovy也支持Java传统的while循环语法: 1 defx =0 2 defy =5 3 4 while( y-- >0) { 5 x++ 6 } 7 8 assertx ==5 for循环 在Groovy中,for循环更加的简单,而且如果你愿意的话,你也可以在Groovy中使用标准的C/Java的for循环语法。 1 for (inti =0; i <5; i++) { 2 } 3 4 // iterate over a range 5 defx =0 6 for ( iin0..9) { 7 x += i 8 } 9 assertx ==45 10 11 // iterate over a list 12 x =0 13 for ( iin[0,1,2,3,4] ) { 14 x += i 15 } 16 assertx ==10 17 18 // iterate over an array 19 array = (0..4).toArray() 20 x =0 21 for ( iinarray ) { 22 x += i 23 } 24 assertx ==10 25 26 // iterate over a map 27 defmap = ['abc':1,'def':2,'xyz':3] 28 x =0 29 for ( einmap ) { 30 x += e.value 31 } 32 assertx ==6 33 34 // iterate over values in a map 35 x =0 36 for ( vinmap.values() ) { 37 x += v 38 } 39 assertx ==6 40 41 // iterate over the characters in a string 42 deftext ="abc" 43 deflist = [] 44 for (cintext) { 45 list.add(c) 46 } 47 assertlist == ["a","b","c"] 闭包(closures) 有时候你也可以使用闭包的each()和eachWithIndex()方法来替换一些for循环代码。 1 defstringList = ["java","perl","python","ruby","c#","cobol", 2 "groovy","jython","smalltalk","prolog","m","yacc"]; 3 4 defstringMap = ["Su":"Sunday","Mo":"Monday","Tu":"Tuesday", 5 "We":"Wednesday","Th":"Thursday","Fr":"Friday", 6 "Sa":"Saturday"]; 7 8 stringList.each() {print" ${it}"};println""; 9 // java perl python ruby c# cobol groovy jython smalltalk prolog m yacc 10 11 stringMap.each() { key, value ->println"${key} == ${value}"}; 12 // Su == Sunday 13 // We == Wednesday 14 // Mo == Monday 15 // Sa == Saturday 16 // Th == Thursday 17 // Tu == Tuesday 18 // Fr == Friday 19 20 stringList.eachWithIndex() { obj, i ->println" ${i}: ${obj}"}; 21 // 0: java 22 // 1: perl 23 // 2: python 24 // 3: ruby 25 // 4: c# 26 // 5: cobol 27 // 6: groovy 28 // 7: jython 29 // 8: smalltalk 30 // 9: prolog 31 // 10: m 32 // 11: yacc 33 34 stringMap.eachWithIndex() { obj, i ->println" ${i}: ${obj}"}; 35 // 0: Su=Sunday 36 // 1: We=Wednesday 37 // 2: Mo=Monday 38 // 3: Sa=Saturday 39 // 4: Th=Thursday 40 // 5: Tu=Tuesday 41 // 6: Fr=Friday 从if-else和try-catch代码块中返回值 从 Groovy 1.6开始,在方法或者闭包中的最后一行表达式,可以从if/else和try/catch/finally代码块中返回值,而且并不需要明确的使用return关键字返回值,只需要他们是代码块的最后一个表达式就行。 下面的例子就说明了这个情况,在下面的代码块中虽然没有显示的调用return关键字,但是仍然会返回1: 1 defmethod() { 2 if(true)1else0 3 } 4 5 assertmethod() ==1 对于 try/catch/finally blocks代码块来说,如果try代码块中没有抛出异常的话,那么try代码块的最后一行的表达式将会被返回,如果try的代码块抛出异常并且被catch住的时候,那么catch代码块中的最后一个表达式的值将会被返回。 但是请注意:finally代码块不会返回值的。 1 defmethod(bool) { 2 try{ 3 if(bool)thrownewException("foo") 4 1 5 }catch(e) { 6 2 7 }finally{ 8 3 9 } 10 } 11 12 assertmethod(false) ==1 13 assertmethod(true) ==2 ============================================================================== 本文转自被遗忘的博客园博客,原文链接:http://www.cnblogs.com/rollenholt/p/3349046.html,如需转载请自行联系原作者