《Groovy in Action》笔记

1、Treat everything as an object and all operators as method calls.

2、Groovy automatically imports the packages groovy.lang.*, groovy.util.*,java.lang.*, java.util.*, java.net.*, and java.io.* as well as the classes java.math.BigInteger and BigDecimal.

3、Methods are public by default.

4、Scripts contain Groovy statements without an enclosing class declaration.

5、Groovy facilitates working with beans in three ways:

■ Generating the accessor methods.

■Allowing simplified access to all JavaBeans (including GroovyBeans).

■ Simplified registration of event handlers.

6、string literals can appear in single or double quotes. The doublequoted version allows the use of placeholders, which are automatically resolved as required.

Eg.

def  nick = 'Gina';
def  book = 'Groovy in Action';
assert  "$nick is $book" == 'Gina is Groovy in Action';


7、List

eg.

def roman = ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII'];
assert roman[4] == 'IV';
roman[18] = 'VIII';
assert roman.size() == 19;
println roman[14];   //null

8、Map

eg.

def http = [100 : 'CONTINUE',200 : 'OK',400 : 'BAD REQUEST' ]
assert http[200] == 'OK'
http[500] = 'INTERNAL SERVER ERROR'
assert http.size() == 4
http[404] = "NOT FOUND";
assert http.size() == 5

9、Ranges

eg.

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

assert (0..<10).contains(9)

assert (0..<10).contains(10) == false


10、Closures

A closure is a piece of code wrapped up as an object. It acts like a method in that it can take parameters and it can return a value. It’s a normal object in that you can pass a reference to it around just as you can a reference to any other object.

eg.

def roman = ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII'];
roman[18] = 'VIII';
roman.each(){
num ->
println num;
}

roman.each{
num ->
println num;
}

def printer = { line -> println line };
Closure c = {str -> println "Closure: " + str};
roman.each(c);
roman.each(printer);

Book book1 = new Book();
book1.setId(1);
book1.setName("groovy in action");

Book book2 = new Book();
book2.setId(2);
book2.setName("advance javascript");

def books = [];

books.add(book1);
books.add(book2);

def i = 0;

def bookClosure = {
book ->
println ((++i) + ".《" + book.getName() + "》");
}

books.each(bookClosure);


def adder = { x, y -> return x+y };
assert adder(4, 3) == 7;

11、In conditionals, null is treated like false; not-null is treated like true.

12、Groovy is dynamic

def un = 2;
println un;

un = new Date();
println un;

13、The default implementation of the equals operator doesn’t throw any NullPointerExceptions either. Remember that == (or equals) denotes object equality (equal values), not identity (same object instances).

14、Method

def doSth(i){    //defalut is public
  if(i){
    return 2;
  }else{
    return new Date();
  }
}// if nothing return then return last executive statement outcomes
 //or null.






转载于:https://my.oschina.net/xiejuntao/blog/150031

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值