《Groovy in Action》笔记

《Groovy in Action》笔记,第1张

概述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 

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() == 4http[404] = "NOT FOUND";assert http.size() == 5

9、Ranges

eg.

def x = 1..10assert x.contains(5)assert x.contains(15) == falseassert x.size() == 10assert x.from == 1assert x.to == 10assert x.reverse() == 10..1assert (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.
总结

以上是内存溢出为你收集整理的《Groovy in Action》笔记全部内容,希望文章能够帮你解决《Groovy in Action》笔记所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1266047.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-08
下一篇2022-06-08

发表评论

登录后才能评论

评论列表(0条)

    保存