情诗网 >情话诗句 > 正文

Groovy,一句话

来源:情诗网    2020-12-29    分类:情话诗句

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

7.return keyword is optional

最后的值会被返回

8.dynamic type

9.ARM blocks

ARM (Automatic Resource Management) block

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

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

12.GroovyBeans && map

13.Autoboxing

14.Strings

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)

17.<=> , Spaceship operator

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

Spaceship operator && Elvis operator 组合, amazing

热门文章