注释:
■ // denotes single-line comments that end with the current line.
■ Multiline comments are enclosed in /* … */ markers.
■ Javadoc-like comments in /** … */ markers are treated the same as other
multiline comments, but support for Groovydoc is in the works at the time
of writing. It will be the Groovy equivalent to Javadoc and will use the
same syntax.
Groovy与Java相似语法:
■ The general packaging mechanism
■ Statements (including package and import statements)
■ Class and method definitions (except for nested classes)
■ Control structures (except the classic for(init;test;inc) loop)
■ Operators, expressions, and assignments
■ Exception handling
■ Declaration of literals (with some twists)
■ Object instantiation, referencing and dereferencing objects, and calling methods
The added value of Groovy’s syntax is to
■ Ease access to the Java objects through new expressions and operators
■ Allow more ways of declaring objects literally
■ Provide new control structures to allow advanced flow control
■ Introduce new datatypes together with their operators and expressions
■ Treat everything as an object
Java:
java.net.URLEncoder.encode("a b");
Groovy:
URLEncoder.encode 'a b'
Groovy automatically imports the packages
groovy.lang.*
groovy.util.*
java.lang.*
java.util.*
java.net.*
java.io.*
java.math.BigInteger
java.math.BigDecimal
GroovyBean:
Generating the accessor methods
Allowing simplified access to all JavaBeans
Simplified registration of event handlers
ex:
class Book {
String title
}
def groovyBook = new Book()
groovyBook.setTitle('Groovy conquers the world')
assert groovyBook.getTitle() == 'Groovy conquers the world'
groovyBook.title = 'Groovy in Action'
assert groovyBook.title == 'Groovy in Action'
正则表达式
declares a pattern with the slashy // syntax
and uses the =~ find operator to match the pattern against a given string.
assert '12334' =~ //d+/
assert 'xxxxx' == '12345'.replaceAll(//d+/, 'x')
number
def x = 1
def y = 2
assert x + y == 3
assert x.plus(y) == 3
assert x instanceof Integer
list map range
list
def roman = ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII']
assert roman[4] == 'IV'
roman[8] = 'VIII'
assert roman.size() == 9
map
def http = [
100 : 'CONTINUE',
200 : 'OK',
400 : 'BAD REQUEST' ]
assert http[200] == 'OK'
http[500] = 'INTERNAL SERVER ERROR'
assert http.size() == 4
range
def x = 1..10
assert x.contains(5)
assert x.contains(15) == false
assert x.size() == 10
assert x.from == 1
assert x.to == 10
assert x.reverse() == 10..1
闭包
control structure
if (false) assert false
if (null)
{
assert false
}
else
{
assert true
}
def i = 0
while (i < 10) {
i++
Groovy’s place in the Java environment 47
}
assert i == 10
def clinks = 0
for (remainingGuests in 0..9) {
clinks += remainingGuests
}
assert clinks == (10*9)/2
def list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for (j in list) {
assert j == list[j]
}
list.each() { item ->
assert item == list[item]
}
switch(3) {
case 1 : assert false; break
case 3 : assert true; break
default: assert false
}
groovyc compile *.groovy to java *.class file