0%

Groovy入门

典型语法

new

$x

in

as

is()

Syntax

Comments

1
2
3
4
5
6
7
//
/*
*/
/**
* 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.Stringgroovy.lang.GString(字符串插值)。

1
2
3
4
5
6
7
8
9
10
'Hello'
"Hello World"
'''
line 1
line 2
'''
"""
line 1
line 2
"""
1
2
def name = 'Ming'
def greeting = "Hello ${name}"

Numbers

  • byte
  • char
  • short
  • int
  • long
  • java.lang.BigInteger
  • float
  • double
  • java.lang.BigDecimal

Booleans

Lists

1
2
3
4
def squares = [1, 4, 9]
def list = [1, "a", true]
LinkedList list1 = [1, 2, 3]
def multi = [[0, 1], [2, 3]]

Arrays

1
String[] arrStr = ['Ananas', 'Banana', 'Kiwi']

Maps

1
2
3
def colors = [red: '#FF0000', green: '#00FF00', blue: '0000FF']
colors[red]
colors.green

Operators

Program Structure

Object orientation

Types

1
2
3
4
5
6
7
8
9
10
11
class Person {

String name
Integer age

def increaseAge(Integer years) {
this.age += years
}
}

def p = new Person()
1
2
3
4
extends
abstract
interface
implements

Groovy支持命名参数、默认参数。

Closure

Semantics

2020-3-8 21:52:42