美文网首页
Groovy,一句话

Groovy,一句话

作者: ThomasLiao | 来源:发表于2018-11-11 20:58 被阅读14次

    Groovy

    Difference with Java

    1.Groovy默认已经import了部分的包和类

    2.multi-methods | runtime dispatch

    groovy:method chosen at runtime base on types of arguments
    java: method chosen at compile time base on declared types

    3.数组初始化

    大括号已经保留给闭包使用。数组的初始化使用中括号

    int[] array = [1,2,3]
    

    4.类的可见域

    java: 默认是 'package private', 只有在同一个包里面才能访问到
    groovy: 默认是public

    5.semicolon is optional

    6.parentheses is optional

    • 普通调用,括号可要可不要
    • 对于closure来说,不写括号是对closure的引用。带括号是对closure的调用
    • 当函数调用嵌套是,需要括号

    7.return keyword is optional

    最后的值会被返回

    • No matter what type of object we try to return, the return type defined in the method will determine the type that is actually returned

    • However, if the method uses the def keyword as its return type, then the return type is considered to be dynamic

    • No matter what value is contained in the last statement in a method, if the return type is void, the value returned will always be null

    8.dynamic type

    • explicit type
    • use def keyword, decide at runtime later

    9.ARM blocks

    ARM (Automatic Resource Management) block

    10.source file contain both class definitions and inline scripting.

    • when compile, generate a class object for each Groovy class
    • generate a class for scripting elements
    • Groovy scripts have a special binding for variable references

    11.GroovyBeans || POGO (Plain Old Groovy Object)

    • Groovy automatically generates getters and setters for instance fields in a class that have the default visibility of public.
    • It also generates a default constructor.
    • field dereference operator @. directly access the field without going through a getter or setter

    12.GroovyBeans && map

    • every groovybean has default built-in Map constructor
    • map中如果包含有不相关的属性,会直接抛异常

    13.Autoboxing

    • as if primitives don't exist
    • treat any numeric value as if both an object-base numeric and a primitive

    14.Strings

    • Normal strings in Groovy are instances of the java.lang.String class
    • Strings that contain the ${...} syntax are instantiated as Groovy GString objects

    15.Closures, on the other hand, can reference variables from outside their own scope

    given: "a variable in scope"
           def greeting = "Hello"
       and: "a closure that can access the variable"
           def greet = { println "$greeting, World!"}
       when: "we invoke the closure with variable different"
           greet()
           greeting = "Goodbye"
           greet()
       then: "the output is as expected"
           """Hello, World!
       Goodbye, World!""" == output()
    
    

    16.Groovy Truth (true/false)

    • 非0都是true
    • null 空值,空串,空的集合都是false, 否则都是true

    17.<=> , Spaceship operator

    Spaceship is a shorthand operator that works the same as Java's compareTo method.

    Spaceship operator && Elvis operator 组合, amazing

    相关文章

      网友评论

          本文标题:Groovy,一句话

          本文链接:https://www.haomeiwen.com/subject/lusffqtx.html