Skip to content

Groovy入门

Posted on:March 8, 2020 at 21:52:20 GMT+8

典型语法

new

$x

in

as

is()

Syntax

Comments

//
/*
	*/
/**
 * Class description
 */
#!/usr/bin/env groovy

Keywords

asassertbreakcase
catchclassconstcontinue
defdefaultdoelse
enumextendsfalsefinally
forgotoifimplements
importininstanceofinterface
newnullpackagereturn
superswitchthisthrow
throwstraittruetry
while

Identifiers

Strings

Groovy中有java.lang.Stringgroovy.lang.GString(字符串插值)。

'Hello'
"Hello World"
'''
line 1
line 2
'''
"""
line 1
line 2
"""
def name = 'Ming'
def greeting = "Hello ${name}"

Numbers

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