当前位置:网站首页>Scope of groovy

Scope of groovy

2022-06-23 13:32:00 Leisurely summer

1、range How to define

Groovy Medium range The definition is a number before and after , And then in the middle .. separation , Represents a range

def range = 1..10 //  Express 1~10

2、Groovy Common methods in

Range Inherited Java In the List Interface , therefore groovy Method of list operation in , Yes range It still applies

2.1、 Get value

range[0]

2.2、contains Method

Check whether the scope contains an element , The return value is of boolean type

range.contains(10)  //  Boolean type 

2.3、from Method to get the upper boundary of the range

range.from

2.4、to Method to get the lower boundary of the range

range.to

3、 Range traversal

range.each { // groovy The way , Recommended 
    println it
}
for (i in range) { // Java The way 
    println i
}

4、 The scope is switch case The use of

def result = getGrade(99)
println result
def getGrade(Number number) {
    def result
    switch (number) {
        case 0..<60:
            result = ' fail, '
            break
        case 60..<70:
            result = ' pass '
            break
        case 70..<80:
            result = ' good '
            break
        case 80..100:
            result = ' good '
            break
    }
    return result  // return It can be omitted 
}

原网站

版权声明
本文为[Leisurely summer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231100545683.html

随机推荐