|
網誌存檔
熱門網志
|
|
Your First Groovy//hello.groovyprintln "hello, world" To run it from command line groovy hello.groovy MyName yourName HisName OverviewGroovy classes compile down to Java bytecode and so there`s a 1-1 mapping between a Groovy class and a Java class. Probably the easiest way to get groovy is to try working with collections. In Groovy List (java.util.List) and Map (java.util.Map) are both first class objects in the syntax. So to create a List of objects you can do the following... def list = [1, 2, `hello`, new java.util.Date()] Notice that everything is an object (or that auto-boxing takes place when working with numbers). To create maps... def map = [`name`:`James`, `location`:`London`] Iterating over collections is easy... def list = [1, 2, 3] Once you have some collections you can then use some of the new collection helper methods or try working with closures... Working with closuresClosures are similar to Java`s inner classes, except they are a single method which is invokable, with arbitrary parameters. A closure can have as many parameters as you wish... def closure = { param -> println("hello ${param}") }If no parameter(s) is(are) specified before -> symbol then a default named parameter, called `it` can be used. e.g. def closure = { println "hello " + it }Using closures allows us to process collections (arrays, maps, strings, files, SQL connections and so forth) in a clean way. e.g [1, 2, 3].each ({ item -> print "${item}-" })Note: If a given closure is the last parameter of a method, its definition can reside outside of the parentheses. Thus the following code is valid: def fun(int i, Closure c) { [1, 2, 3].each ({ item -> print "${item}-" }) // 1-2-3- // omit enclosing () [1, 2, 3].each { item -> print "${item}-" } // 1-2-3- // normal [1, 2, 3].each(({ item -> print "${item}-" })) // 1-2-3- // using the fun function to do the same thing [1,2,3].each {fun(it,{item -> print "${item}-"})} // 1-2-3- def closure = { i -> println i} //[1, 2, 3].each() closure // error. closure has been previously defined Here are a number of helper methods available on collections & strings... collectcollect the return value of calling a closure on each item in a collection def value = [1, 2, 3].collect { it * 2 }findfinds first item matching closure predicate def value = [1, 2, 3].find { it > 1 }findAllfinds all items matching closure predicate def value = [1, 2, 3].findAll { it > 1 }injectallows you to pass a value into the first iteration and then pass the result of that iteration into the next iteration and so on. This is ideal for counting and other forms of processing def value = [1, 2, 3].inject(`counting: `) { str, item -> str + item }In addition there`s 2 new methods for doing boolean logic on some collection... everyreturns true if all items match the closure predicate def value = [1, 2, 3].every { it < 5 }anyreturns true if any item match the closure predicate def value = [1, 2, 3].any { it > 2 }Other helper methods include: |
-------------------------------------------------
| 上一篇:install Groovy in RedHat as4 | 下一篇:lucene简单实例<二> |

