groovy the switch / case Analyzing

switch/case

1  // the Java in the switch can only pass an int, byte, char and short types can be automatically promoted to int type, String type, and later extended the enum type
 2  
3  // the groovy, switch wayward types of data can be passed match 
. 4  String judgeType (Object X) {
 . 5      DEF Result
 . 6      Switch (X) {
 . 7          Case "String" :
 . 8              Result = "X IS String"
 . 9              BREAK 
10          Case [. 4,. 5,. 6,. 7, 'inList'] : // list (to explain the data structure) 
. 11              Result = "X IS in list [. 4,. 5,. 6,. 7, 'inList']"
 12 is              BREAK 
13 is          Case 10..15: // range range (to explain the data structure )
14             result = "x is in range 10..15"
15             break
16         case Integer:
17             result = "x is Integer"
18             break
19         case BigDecimal:
20             result = "x is BigDecimal"
21             break
22         case List:
23             result = "x is List"
24             break
25         default:
26             result = "no match"
27             break
28     }
29     return result
30 }
31 
32 def x = "string"
33 def x1 = 5
34 def x2 = 10
35 def x3 = 3
36 def x4 = 3.14
37 def x5 =[4,6]
38 def x6 ="hi groovy"
39 def x7 = "inList"
40 println judgeType(x) //x is string
41 println judgeType(x1) // x is in list [4, 5, 6, 7,'inList']
42 println judgeType(x2) // x is in range 10..15
43 println judgeType(x3) // x is Integer
44 println judgeType(x4) // x is BigDecimal
45 println judgeType(x5) // x is List
46 println judgeType(x6) // no match
47 println judgeType(x7) // x is in list [4, 5, 6, 7,'inList']

 

Guess you like

Origin www.cnblogs.com/benhuli/p/11883678.html