phoenix13

导航

 

1. print

println "Hello Groovy!"

you can use java in Groovy

System.out.println("Hello Groovy!");

2. define variable

Groovy is dynamically typed, type is optional

def foo = 6.5

you can define type if you want 

int foo2=7

3. use expression in string

variable or expression start with dollar sign inside of the string

// variable in string
println "foo has value: $foo"
// expression in string
println "Let's do some math. 5 + 6 = ${5 + 6}"
// foo is variable, abc is literal
println "${foo+"abc"}"

4. define function

def doubleIt(n) {
    n + n // Note we don't need a return statement
}

5. call function

for functions with 1+ args, we can call it without parenthese

def noArgs() {
    println "Called the no args function"
}

def oneArg(x) {
    println "Called the 1 arg function with $x"
    x
}

def twoArgs(x, y) {
    println "Called the 2 arg function with $x and $y"
    x + y
}

oneArg 500 // Look, Ma! No parentheses!
twoArgs 200, 300
noArgs()
//noArgs // Doesn't work
//twoArgs oneArg 500, 200 // Also doesn't work as it's ambiguous
twoArgs oneArg(500), 200 // Fixed the ambiguity

 

posted on 2015-10-22 15:03  phoenix13  阅读(192)  评论(0编辑  收藏  举报