算术和条件运算符
Groovy支”!”操作符,例如:
1 |
def expression = false |
2 |
assert !expression |
基于集合的运算符:
Spread Operator (*.)运算法:
spread操作符用来调用集合中的每一个对象的行为,就相当于调用collect方法一样:
1 |
parent*.action //equivalent to: |
2 |
parent. collect { child -> child?.action } |
这个行为可能是一个方法调用或者属性访问,并返回一个列表。下面是一个例子:
1 |
assert [ 'cat' , 'elephant' ]*. size () == [ 3 , 8 ] |
我们也可以自定义这个运算符的行为:
1 |
class Person { String name } |
2 |
class Twin { |
3 |
Person one, two |
4 |
def iterator() { |
5 |
return [one, two].iterator() |
6 |
} |
7 |
} |
8 |
9 |
def tw = new Twin(one: new Person(name: 'Tom' ), |
10 |
two: new Person(name: 'Tim' )) |
11 |
assert tw*.name == [ 'Tom' , 'Tim' ] |
12 |
// expanded equivalent of above: |
13 |
assert tw. collect { it.name } == [ 'Tom' , 'Tim' ] |
Java field (.@):
Groovy自动的为所有的字段属性创建getter方法来返回字段属性的引用:
1 |
class X |
2 |
{ |
3 |
def field |
4 |
} |
5 |
6 |
x = new X() |
7 |
x.field = 1 |
8 |
println x.field // 1 |
当然,你也可以根据自己的需求重载这个getter方法:
1 |
class X |
2 |
{ |
3 |
def field |
4 |
5 |
def getField() |
6 |
{ |
7 |
field += 1 |
8 |
} |
9 |
} |
10 |
11 |
x = new X() |
12 |
x.field = 1 |
13 |
println x.field // 2 |
@运算符容许你重载这个行为并且直接访问字段,所以我们可以将上面的代码扩展为:
1 |
println x.@field // 1 |
但是并不推荐这种方法。官方给我解释是:
It should be mentioned that, while interesting, this is probably not a good thing to do unless you really need to. Overriding a public interface to access the internal state of an object probably means you are about to break something. Not even recommended for use in tests since it increases coupling unnecessarily.
其他的操作符:
getAt(index)和putAt(index, value)对应于下标操作符,比如 foo[1] or foo['bar'],需要注意的是下标可以是int也可以是string。
Range操作符(..)类似于Python中的range函数,容许你创建一个序列。
isCase()操作符对应于in
Elvis Operator (?: )
这个操作符是java中三元操作符的另外一种形式,比如:
1 |
def displayName = user.name ? user.name : "Anonymous" //traditional ternary operator usage |
2 |
3 |
def displayName = user.name ?: "Anonymous" // more compact Elvis operator - does same as above |
Safe Navigation Operator (?.)
好吧,我把它翻译为安全导航操作符,这个操作符主要是为了避免在你访问一个对象的属性或者方法的时候,出现 NullPointerException的问题。在java中当我们访问一个对象(这个对象值为null)的属性或者方法,会出现空指针异常,如果使用这个操作符,就不会出现空指针异常,而是会返回null。
1 |
def user = User. find ( "admin" ) //this might be null if 'admin' does not exist |
2 |
def streetName = user?.address?.street //streetName will be null if user or user.address is null - no NPE thrown |
正则表达式操作符
- find (=~)
- match (==~)
这两个操作符以后再说。 下面的表格列出了大量的操作符:
Operator Name |
Symbol |
Description |
Spaceship |
<=> |
Useful in comparisons, returns -1 if left is smaller 0 if == to right or 1 if greater than the right |
Regex find |
=~ |
Find with a regular expresion? See Regular Expressions |
Regex match |
==~ |
Get a match via a regex? See Regular Expressions |
.@ |
Can be used to override generated properties to provide access to a field |
|
*. |
Used to invoke an action on all items of an aggregate object |
|
Spread Java Field |
*.@ |
Amalgamation of the above two |
Method Reference |
.& |
Get a reference to a method, can be useful for creating closures from methods |
asType Operator |
as |
Used for groovy casting, coercing one type to another. |
Membership Operator |
in |
Can be used as replacement for collection.contains() |
Identity Operator |
is |
Identity check. Since == is overridden in Groovy with the meaning of equality we need some fallback to check for object identity. |
?. |
returns nulls instead of throwing NullPointerExceptions |
|
?: |
Shorter ternary operator |