zoukankan      html  css  js  c++  java
  • Groovy basic

    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
  • 相关阅读:
    luogu 1593
    luogu 1369
    hdu 1796
    bzoj 3398
    luogu 4587
    luogu 2152
    bzoj 3629
    bzoj 1507: [NOI2003]Editor
    bzoj 1503: [NOI2004]郁闷的出纳员
    bzoj 1497: [NOI2006]最大获利
  • 原文地址:https://www.cnblogs.com/phoenix13suns/p/4901042.html
Copyright © 2011-2022 走看看