Groovy in action读书笔记1

注释:
■ // 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值