zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    Python 基础 (三)
    Python 基础(二)
    软件工程第四周作业
    软件工程第四周作业
    软件工程第四次作业
    软件工程第四周作业
    软件工程第三次作业
    软件工程第三周作业
    软件工程第三次作业
    Python学习之路5
  • 原文地址:https://www.cnblogs.com/rollenholt/p/3349052.html
Copyright © 2011-2022 走看看