groovy語句類似於java語句,但是在groovy中的分號”;”是可選的。比如:
1 |
def x = [ 1 , 2 , 3 ] |
2 |
println x |
3 |
def y = 5 ; def x = y + 7 |
4 |
println x |
5 |
assert x == 12 |
而且對於一些方法參數等復雜的事情,我們可以橫跨多行:
1 |
def x = [ 1 , 2 , 3 , |
2 |
4 , 5 , 6 ] |
3 |
println ( |
4 |
x |
5 |
) |
6 |
if (x != null && |
7 |
x. size () > 5 ) { |
8 |
println ( "Works!" ) |
9 |
} |
10 |
else { |
11 |
assert false: "should never happen ${x}" |
12 |
} |
groovy支持一行給多個變量賦值:
1 |
def (a, b) = [ 1 , 2 ] |
2 |
3 |
assert a == 1 |
4 |
assert b == 2 |
這就使得我們的方法可以返回多個值了,比如返回經緯度的方法:
1 |
def geocode(String location) { |
2 |
// implementation returns [48.824068, 2.531733] for Paris, France |
3 |
[ 48.824068 , 2.531733 ] |
4 |
} |
5 |
6 |
def (_lat, _long) = geocode( "Paris, France" ) |
7 |
8 |
assert _lat == 48.824068 |
9 |
assert _long == 2.531733 |
當然我們也可以定義方法的參數類型:
1 |
def ( int i, String s) = [ 1 , 'Groovy' ] |
2 |
3 |
assert i == 1 |
4 |
assert s == 'Groovy' |
對於事先已經定義好的變量,我們在賦值的時候不需要def關鍵字:
1 |
def firstname, lastname |
2 |
3 |
(firstname, lastname) = "Guillaume Laforge" . tokenize () |
4 |
5 |
assert firstname == "Guillaume" |
6 |
assert lastname == "Laforge" |
當然,在賦值的時候可能會出現兩側的數量不一致的情況,比如當左側數量多於右側的時候,左側多出來的為null:
1 |
def elements = [ 1 , 2 ] |
2 |
def (a, b, c) = elements |
3 |
4 |
assert a == 1 |
5 |
assert b == 2 |
6 |
assert c == null |
但是當右側的多於左側的時候,多出來的不賦值。
1 |
def elements = [ 1 , 2 , 3 , 4 ] |
2 |
def (a, b, c) = elements |
3 |
4 |
assert a == 1 |
5 |
assert b == 2 |
6 |
assert c == 3 |
根據groovy的語法,我們可以在一行中swap兩個變量:
1 |
// given those two variables |
2 |
def a = 1 , b = 2 |
3 |
4 |
// swap variables with a list |
5 |
(a, b) = [b, a] |
6 |
7 |
assert a == 2 |
8 |
assert b == 1 |
注釋:
1 |
print "hello" // This is a silly print statement |
2 |
3 |
/* This is a long comment |
4 |
about our favorite println */ |
5 |
println "hello" |
6 |
7 |
// This doesn't work: |
8 |
# Bad comment |
我們可以發現#其實並不是注釋字符。
方法調用
groovy中的方法調用類似於java,比如:
1 |
class Foo { |
2 |
def calculatePrice() { |
3 |
1.23 |
4 |
} |
5 |
6 |
static void main(args) { |
7 |
def foo = new Foo() |
8 |
def p = foo.calculatePrice() |
9 |
assert p > 0 |
10 |
11 |
println "Found price: " + p |
12 |
} |
13 |
} |
可選的括號
在groovy中,Groovy中的方法調用可以省略括號,如果有至少一個參數,並且不存在任何含糊。比如:
1 |
println "Hello world" |
2 |
System.out. println "Nice cheese Gromit!" |
在命名參數的時候,也是可以省略的:
1 |
compare fund: "SuperInvestment" , withBench: "NIKEI" |
2 |
monster.move from: [ 3 , 4 ], to: [ 4 , 5 ] |
命名參數傳遞
當調用一個方法時,你可以通過在命名參數。參數名稱和值之間由一個冒號,比如:
1 |
def bean = new Expando(name: "James" , location: "London" , id: 123 ) |
2 |
println "Hey " + bean.name |
3 |
assert bean.id == 123 |
給方法傳遞閉包
閉包也可以像其他對象一樣傳遞給方法:
1 |
def closure = { param -> param + 1 } |
2 |
def answer = [ 1 , 2 ]. collect (closure) |
3 |
assert answer == [ 2 , 3 ] |
上面的代碼等價於:
1 |
answer = [ 1 , 2 ]. collect { param -> param + 1 } |
2 |
assert answer == [ 2 , 3 ] |
屬性
為了訪問屬性你可以使用屬性名和.:
1 |
def bean = new Expando(name: "James" , location: "London" , id: 123 ) |
2 |
def name = bean.name |
3 |
println ( "Hey ${name}" ) |
4 |
bean.location = "Vegas" |
5 |
println bean.name + " is now in " + bean.location |
6 |
assert bean.location == "Vegas" |
安全導航
如果你在訪問屬性的時候,避免出現空指針異常的話,那么安全導航操作符可能適合你:
1 |
def foo = null |
2 |
def bar = foo?.something?.myMethod() |
3 |
assert bar == null |