groovy-语句

groovy-语句


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

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

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

1 def x = [123,
2  456]
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) = [12]
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.8240682.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 = [12]
2 def (a, b, c) = elements
3 
4 assert a == 1
5 assert b == 2
6 assert c == null

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

1 def elements = [1234]
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 = [12].collect(closure)
3 assert answer == [23]

上面的代码等价于:

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

属性

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

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

 


==============================================================================
本文转自被遗忘的博客园博客,原文链接:http://www.cnblogs.com/rollenholt/p/3349052.html,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值