典型语法
new
$x
in
as
is()
Syntax
Comments
//
/*
*/
/**
* Class description
*/
#!/usr/bin/env groovy
Keywords
as | assert | break | case |
---|---|---|---|
catch | class | const | continue |
def | default | do | else |
enum | extends | false | finally |
for | goto | if | implements |
import | in | instanceof | interface |
new | null | package | return |
super | switch | this | throw |
throws | trait | true | try |
while |
Identifiers
Strings
Groovy中有java.lang.String
和groovy.lang.GString
(字符串插值)。
'Hello'
"Hello World"
'''
line 1
line 2
'''
"""
line 1
line 2
"""
def name = 'Ming'
def greeting = "Hello ${name}"
Numbers
byte
char
short
int
long
java.lang.BigInteger
float
double
java.lang.BigDecimal
Booleans
Lists
def squares = [1, 4, 9]
def list = [1, "a", true]
LinkedList list1 = [1, 2, 3]
def multi = [[0, 1], [2, 3]]
Arrays
String[] arrStr = ['Ananas', 'Banana', 'Kiwi']
Maps
def colors = [red: '#FF0000', green: '#00FF00', blue: '0000FF']
colors[red]
colors.green
Operators
Program Structure
Object orientation
Types
class Person {
String name
Integer age
def increaseAge(Integer years) {
this.age += years
}
}
def p = new Person()
extends
abstract
interface
implements
Groovy支持命名参数、默认参数。
Closure
Semantics
待
2020-3-8 21:52:42