groovy-语句

groovy语句类似于java语句,但是在groovy中的分号”;”是可选的。比如:

1def x = [123]
2println x
3def y = 5def x = y + 7
4println x
5assert x == 12

而且对于一些方法参数等复杂的事情,我们可以横跨多行:

1def x = [123,
2 456]
3println(
4 x
5)
6if (x != null &&
7 x.size() > 5) {
8 println("Works!")
9}
10else {
11 assert false: "should never happen ${x}"
12}

groovy支持一行给多个变量赋值:

1def (a, b) = [12]
2 
3assert a == 1
4assert b == 2

这就使得我们的方法可以返回多个值了,比如返回经纬度的方法:

1def geocode(String location) {
2 // implementation returns [48.824068, 2.531733] for Paris, France
3 [48.8240682.531733]
4}
5 
6def (_lat, _long) = geocode("Paris, France")
7 
8assert _lat == 48.824068
9assert _long == 2.531733

当然我们也可以定义方法的参数类型:

1def (int i, String s) = [1'Groovy']
2 
3assert i == 1
4assert s == 'Groovy'

对于事先已经定义好的变量,我们在赋值的时候不需要def关键字:

1def firstname, lastname
2 
3(firstname, lastname) = "Guillaume Laforge".tokenize()
4 
5assert firstname == "Guillaume"
6assert lastname == "Laforge"

当然,在赋值的时候可能会出现两侧的数量不一致的情况,比如当左侧数量多于右侧的时候,左侧多出来的为null:

1def elements = [12]
2def (a, b, c) = elements
3 
4assert a == 1
5assert b == 2
6assert c == null

但是当右侧的多于左侧的时候,多出来的不赋值。

1def elements = [1234]
2def (a, b, c) = elements
3 
4assert a == 1
5assert b == 2
6assert c == 3

根据groovy的语法,我们可以在一行中swap两个变量:

1// given those two variables
2def a = 1, b = 2
3 
4// swap variables with a list
5(a, b) = [b, a]
6 
7assert a == 2
8assert b == 1

注释:

1print "hello" // This is a silly print statement
2 
3/* This is a long comment
4 about our favorite println */
5println "hello"
6 
7// This doesn't work:
8# Bad comment

我们可以发现#其实并不是注释字符。

方法调用

groovy中的方法调用类似于java,比如:

1class Foo {
2 def calculatePrice() {
3 1.23
4 }
5 
6static void main(args) {
7 def foo = new Foo()
8 def p = foo.calculatePrice()
9 assert p > 0
10 
11println "Found price: " + p
12 }
13}

可选的括号

在groovy中,Groovy中的方法调用可以省略括号,如果有至少一个参数,并且不存在任何含糊。比如:

1println "Hello world"
2System.out.println "Nice cheese Gromit!"

在命名参数的时候,也是可以省略的:

1compare fund: "SuperInvestment", withBench: "NIKEI"
2monster.move from: [3,4], to: [4,5]

命名参数传递

当调用一个方法时,你可以通过在命名参数。参数名称和值之间由一个冒号,比如:

1def bean = new Expando(name:"James", location:"London", id:123)
2println "Hey " + bean.name
3assert bean.id == 123

给方法传递闭包

闭包也可以像其他对象一样传递给方法:

1def closure = { param -> param + 1 }
2def answer = [12].collect(closure)
3assert answer == [23]

上面的代码等价于:

1answer = [12].collect { param -> param + 1 }
2assert answer == [23]

属性

为了访问属性你可以使用属性名和.:

1def bean = new Expando(name:"James", location:"London", id:123)
2def name = bean.name
3println("Hey ${name}")
4bean.location = "Vegas"
5println bean.name + " is now in " + bean.location
6assert bean.location == "Vegas"

安全导航

如果你在访问属性的时候,避免出现空指针异常的话,那么安全导航操作符可能适合你:

1def foo = null
2def bar = foo?.something?.myMethod()
3assert bar == null

 

转载于:https://www.cnblogs.com/rollenholt/p/3349052.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值