Groovy,一句话

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,448评论 0 10
  • Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de F...
    苏黎九歌阅读 13,906评论 0 38
  • 【长郡中学】 长郡:市理科状元;600分以上480人,比全省第二名多118人;北大、清华预计60人;一本上线率91...
    老师课堂阅读 1,265评论 0 0
  • 听了一天的课,感觉很烧脑。 课程的题目是《万物生长靠太阳》,内容其实是围绕《零极限》的观点展开。 授课老师是对中国...
    心际流尘阅读 716评论 0 0
  • 在flask默认的启动中app.run(port=1234)就能换个端口访问而使用manage则不行,不过也更简单...
    晓函阅读 1,828评论 0 0