http://groovy.codehaus.org/

  1. "def" is a replacement for a type name. In variable definitions it is used to indicate that you don't care about the type. In variable definitions it is mandatory to either provide a type name explicitly or to use "def" in replacement. This is needed to the make variable definitions detectable for the Groovy parser.
  2. 1
    def name='World'; println "Hello $name!"
  3. 面向对象
    1
    2
    3
    4
    5
    6
    7
    class Greet{
        def name
        Greet(who){name=who[0].toUpperCase()+who[1..-1]}
        def salute(){println "hello $name!"}
    }
    g = new Greet('world')
    g.salute()
  4. 引入其他类库
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    import static org.apache.commons.lang3.text.WordUtils.*
     
    class Greeter {
        def name
        Greeter(who) { name = capitalize(who) }
        def salute(){
            println "Hello $name!"
        }
    }
     
    new Greeter('world').salute()
  5. The recommended way for making Groovy be aware of your additional jar files is to place them in a predefined location. Your Groovy install should include a file called groovy-starter.conf. Within that file, make sure a line such as

    load ${user.home}/.groovy/lib/*
    
  6. groovyConsole: Ctrl+R 执行当前代码
    1
    2
    3
    4
    5
    groovy> println "Hello World !"
    Hello World !
     
    groovy> 123+45*89 
    Result: 4128
  7. Variables:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    x = 1
    println x
     
    x = new java.util.Date()
    println x
     
    x = -3.1499392
    println x
     
    x = false
    println x
     
    x = "Hi"
    println x
  8. Lists and Maps:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    myList = [1776, -1, 33, 99, 0, 928734928763]
    println myList[0]
     
    scores = [ "Brett":100, "Pete":"Did not finish", "Andrew":86.87934 ]
    println scores["Pete"]
    println scores.Pete
    scores["Pete"] = 3
     
    emptyMap = [:]
    emptyList = []

     

  9. Conditional Execution

    1
    2
    3
    4
    5
    6
    7
    amPM = Calendar.getInstance().get(Calendar.AM_PM)
    if (amPM == Calendar.AM)
    {
        println("Good morning")
    } else {
        println("Good evening")
    }

     

  10. Boolean Expressions:

    1
    2
    3
    4
    5
    6
    * ==
    * !=
    * >
    * >=
    * <
    * <=

     

  11. Debugging and Troubleshooting Tips:

    • Print out the class of a variable that you're interested in with myVar.getClass(). Then look up the documentation for that class.
    • If you're having trouble with a complex expression, pare it down to a simpler expression and evaluate that. Then build up to your more complex expression.
    • Try restarting the groovyConsole (this will clear out all the variables so you can start over.
    • Look for the topic you're interested in in the Groovy User Guide
  12. Strings

    • Strings are not Lists. In the JVM java.lang.String does not implement java.util.List.
    • Arrays are not Lists. In the JVM arrays and java.util.List are quite different. In Groovy we support both as different types to ensure we interoperate cleanly with Java code, though we try wherever possible to make them interchangable and appear polymorphic.

    Maps

    • Maps override the dot operator, so myMap.size will return null unless you have a value for map[size]. Use map.size() instead.
    • In map literals, all keys are interpreted as strings by default! If you want to use a variable or other literal as a key, use parentheses like so: myMap = [(var1):val, (var2):val]
    • See the Maps user guide
  13. http://groovy.codehaus.org/gapi/

posted on   架构师师  阅读(467)  评论(0编辑  收藏  举报

编辑推荐:
· .NET 9 new features-C#13新的锁类型和语义
· Linux系统下SQL Server数据库镜像配置全流程详解
· 现代计算机视觉入门之:什么是视频
· 你所不知道的 C/C++ 宏知识
· 聊一聊 操作系统蓝屏 c0000102 的故障分析
阅读排行:
· DeepSeek V3 两周使用总结
· 回顾我的软件开发经历(1)
· C#使用yield关键字提升迭代性能与效率
· 低成本高可用方案!Linux系统下SQL Server数据库镜像配置全流程详解
· 4. 使用sql查询excel内容

导航

< 2025年1月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8

统计

点击右上角即可分享
微信分享提示