《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 = ['','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 doesnt 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.

相关文章

背景:    8月29日,凌晨4点左右,某服务告警,其中一个...
https://support.smartbear.comeadyapi/docs/soapui/steps/g...
有几个选项可用于执行自定义JMeter脚本并扩展基线JMeter功能...
Scala和Java为静态语言,Groovy为动态语言Scala:函数式编程,...
出处:https://www.jianshu.com/p/ce6f8a1f66f4一、一些内部...
在运行groovy的junit方法时,报了这个错误:java.lang.Excep...