groovy运算符

import java.util.regex.Matcher

/**
 * Created by Jxy on 2018/12/20 10:29
 * groovy运算符
 */
/*class operator {
    static void main(String ...args){
        assert -(-1) == 1
        assert (!'foo')   == false
        assert (!'')      == true

    }

}*/
class User {
    public final String name
    User(String name) { this.name = name}
    String getName() { "Name: $name" }
}
def user = new User('jser')
assert user.name == 'Name: jser'
/*
use of .@ forces usage of the field instead of the getter
 */
assert user.@name == 'Bob'
println(user.@name)

/*
方法指针
重载方法
 */
def doSomething(String str ){
    str.toUpperCase()
}
def doSomething(Integer integer){
    integer*2
}

def action = this.&doSomething(24)
println(action)

/*
查找运算符
 */
def text = "some text to match"
def m = text =~ /match/
assert m instanceof Matcher
if (!m) {
    throw new RuntimeException("Oops, text not found!")
}

m = text ==~ /match/
assert m instanceof Boolean
/*
操作符(*.)
 */
class Car {
    String make
    String model
}
def cars = [
        new Car(make: 'Peugeot', model: '508'),
        new Car(make: 'Renault', model: 'C00o')]
def makes = cars.collect{ it.make }//cars*.make
assert makes == ['Peugeot', 'Renault']

/*
传递操作符(*)
 */
int function(int x, int y, int z) {
    x*y+z
}
def list = [5,6,7]
def args = [4]
println(function(4,5,*args))
println(function(*list))

/*
范围运算符
 */
def range = 0..5
assert range.collect() == [0, 1, 2, 3, 4, 5]
assert (0..<5).collect() == [0, 1, 2, 3, 4]
assert (0..5) instanceof List
assert (0..5).size() == 6
原文地址:https://www.cnblogs.com/jsersudo/p/10150448.html