zh-cn translation for groovy

This commit is contained in:
Todd Gao 2015-06-19 10:58:39 +08:00
parent 6b109e1460
commit 021c80723e

View File

@ -1,36 +1,38 @@
--- ---
language: Groovy language: Groovy
filename: learngroovy.groovy filename: learngroovy-cn.groovy
contributors: contributors:
- ["Roberto Pérez Alcolea", "http://github.com/rpalcolea"] - ["Roberto Pérez Alcolea", "http://github.com/rpalcolea"]
filename: learngroovy.groovy translators:
- ["Todd Gao", "http://github.com/7c00"]
lang: zh-cn
--- ---
Groovy - A dynamic language for the Java platform [Read more here.](http://www.groovy-lang.org/) Groovy - Java平台的动态语言。[了解更多。](http://www.groovy-lang.org/)
```groovy ```groovy
/* /*
Set yourself up: 安装:
1) Install GVM - http://gvmtool.net/ 1) 安装 GVM - http://gvmtool.net/
2) Install Groovy: gvm install groovy 2) 安装 Groovy gvm install groovy
3) Start the groovy console by typing: groovyConsole 3) 启动 groovy 控制台,键入: groovyConsole
*/ */
// Single line comments start with two forward slashes // 双斜线开始的是单行注释
/* /*
Multi line comments look like this. 像这样的是多行注释
*/ */
// Hello World // Hello World
println "Hello world!" println "Hello world!"
/* /*
Variables: 变量:
You can assign values to variables for later use 可以给变量赋值,稍后再用
*/ */
def x = 1 def x = 1
@ -49,142 +51,137 @@ x = "Groovy!"
println x println x
/* /*
Collections and maps 集合和map
*/ */
//Creating an empty list //创建一个空的列表
def technologies = [] def technologies = []
/*** Adding a elements to the list ***/ /*** 往列表中增加一个元素 ***/
// As with Java // 和Java一样
technologies.add("Grails") technologies.add("Grails")
// Left shift adds, and returns the list // 左移添加,返回该列表
technologies << "Groovy" technologies << "Groovy"
// Add multiple elements // 增加多个元素
technologies.addAll(["Gradle","Griffon"]) technologies.addAll(["Gradle","Griffon"])
/*** Removing elements from the list ***/ /*** 从列表中删除元素 ***/
// As with Java // 和Java一样
technologies.remove("Griffon") technologies.remove("Griffon")
// Subtraction works also // 减法也行
technologies = technologies - 'Grails' technologies = technologies - 'Grails'
/*** Iterating Lists ***/ /*** 遍历列表 ***/
// Iterate over elements of a list // 遍历列表中的元素
technologies.each { println "Technology: $it"} technologies.each { println "Technology: $it"}
technologies.eachWithIndex { it, i -> println "$i: $it"} technologies.eachWithIndex { it, i -> println "$i: $it"}
/*** Checking List contents ***/ /*** 检查列表内容 ***/
//Evaluate if a list contains element(s) (boolean) //判断列表是否包含某元素返回boolean
contained = technologies.contains( 'Groovy' ) contained = technologies.contains( 'Groovy' )
// Or //
contained = 'Groovy' in technologies contained = 'Groovy' in technologies
// Check for multiple contents // 检查多个元素
technologies.containsAll(['Groovy','Grails']) technologies.containsAll(['Groovy','Grails'])
/*** Sorting Lists ***/ /*** 排序列表 ***/
// Sort a list (mutates original list) // 排序列表(修改原列表)
technologies.sort() technologies.sort()
// To sort without mutating original, you can do: // 要想不修改原列表,可以这样:
sortedTechnologies = technologies.sort( false ) sortedTechnologies = technologies.sort( false )
/*** Manipulating Lists ***/ /*** 操作列表 ***/
//Replace all elements in the list //替换列表元素
Collections.replaceAll(technologies, 'Gradle', 'gradle') Collections.replaceAll(technologies, 'Gradle', 'gradle')
//Shuffle a list //打乱列表
Collections.shuffle(technologies, new Random()) Collections.shuffle(technologies, new Random())
//Clear a list //清空列表
technologies.clear() technologies.clear()
//Creating an empty map //创建空的map
def devMap = [:] def devMap = [:]
//Add values //增加值
devMap = ['name':'Roberto', 'framework':'Grails', 'language':'Groovy'] devMap = ['name':'Roberto', 'framework':'Grails', 'language':'Groovy']
devMap.put('lastName','Perez') devMap.put('lastName','Perez')
//Iterate over elements of a map //遍历map元素
devMap.each { println "$it.key: $it.value" } devMap.each { println "$it.key: $it.value" }
devMap.eachWithIndex { it, i -> println "$i: $it"} devMap.eachWithIndex { it, i -> println "$i: $it"}
//Evaluate if a map contains a key //判断map是否包含某键
assert devMap.containsKey('name') assert devMap.containsKey('name')
//Evaluate if a map contains a value //判断map是否包含某值
assert devMap.containsValue('Roberto') assert devMap.containsValue('Roberto')
//Get the keys of a map //取得map所有的键
println devMap.keySet() println devMap.keySet()
//Get the values of a map //取得map所有的值
println devMap.values() println devMap.values()
/* /*
Groovy Beans Groovy Beans
GroovyBeans are JavaBeans but using a much simpler syntax GroovyBeans 是 JavaBeans但使用了更简单的语法
When Groovy is compiled to bytecode, the following rules are used. Groovy 被编译为字节码时,遵循下列规则。
* If the name is declared with an access modifier (public, private or * 如果一个名字声明时带有访问修饰符public, private, 或者 protected
protected) then a field is generated. 则会生成一个字段field
* A name declared with no access modifier generates a private field with * 名字声明时没有访问修饰符则会生成一个带有public getter和setter的
public getter and setter (i.e. a property). private字段即属性property
* If a property is declared final the private field is created final and no * 如果一个属性声明为final则会创建一个final的private字段但不会生成setter。
setter is generated.
* You can declare a property and also declare your own getter or setter. * 可以声明一个属性的同时定义自己的getter和setter。
* You can declare a property and a field of the same name, the property will * 可以声明具有相同名字的属性和字段,该属性会使用该字段。
use that field then.
* If you want a private or protected property you have to provide your own * 如果要定义private或protected属性必须提供声明为private或protected的getter
getter and setter which must be declared private or protected. 和setter。
* If you access a property from within the class the property is defined in * 如果使用显式或隐式的 this例如 this.foo, 或者 foo访问类的在编译时定义的属性
at compile time with implicit or explicit this (for example this.foo, or Groovy会直接访问对应字段而不是使用getter或者setter
simply foo), Groovy will access the field directly instead of going though
the getter and setter.
* If you access a property that does not exist using the explicit or * 如果使用显式或隐式的 foo 访问一个不存在的属性Groovy会通过元类meta class
implicit foo, then Groovy will access the property through the meta class, 访问它,这可能导致运行时错误。
which may fail at runtime.
*/ */
class Foo { class Foo {
// read only property // 只读属性
final String name = "Roberto" final String name = "Roberto"
// read only property with public getter and protected setter // 只读属性有public getter和protected setter
String language String language
protected void setLanguage(String language) { this.language = language } protected void setLanguage(String language) { this.language = language }
// dynamically typed property // 动态类型属性
def lastName def lastName
} }
/* /*
Logical Branching and Looping 逻辑分支和循环
*/ */
//Groovy supports the usual if - else syntax //Groovy支持常见的if - else语法
def x = 3 def x = 3
if(x==1) { if(x==1) {
@ -195,32 +192,32 @@ if(x==1) {
println "X greater than Two" println "X greater than Two"
} }
//Groovy also supports the ternary operator: //Groovy也支持三元运算符
def y = 10 def y = 10
def x = (y > 1) ? "worked" : "failed" def x = (y > 1) ? "worked" : "failed"
assert x == "worked" assert x == "worked"
//For loop //for循环
//Iterate over a range //使用区间range遍历
def x = 0 def x = 0
for (i in 0 .. 30) { for (i in 0 .. 30) {
x += i x += i
} }
//Iterate over a list //遍历列表
x = 0 x = 0
for( i in [5,3,2,1] ) { for( i in [5,3,2,1] ) {
x += i x += i
} }
//Iterate over an array //遍历数组
array = (0..20).toArray() array = (0..20).toArray()
x = 0 x = 0
for (i in array) { for (i in array) {
x += i x += i
} }
//Iterate over a map //遍历map
def map = ['name':'Roberto', 'framework':'Grails', 'language':'Groovy'] def map = ['name':'Roberto', 'framework':'Grails', 'language':'Groovy']
x = 0 x = 0
for ( e in map ) { for ( e in map ) {
@ -228,54 +225,52 @@ for ( e in map ) {
} }
/* /*
Operators 运算符
Operator Overloading for a list of the common operators that Groovy supports: Groovy中下列运算符支持重载
http://www.groovy-lang.org/operators.html#Operator-Overloading http://www.groovy-lang.org/operators.html#Operator-Overloading
Helpful groovy operators 实用的groovy运算符
*/ */
//Spread operator: invoke an action on all items of an aggregate object. //展开spread运算符对聚合对象的所有元素施加操作
def technologies = ['Groovy','Grails','Gradle'] def technologies = ['Groovy','Grails','Gradle']
technologies*.toUpperCase() // = to technologies.collect { it?.toUpperCase() } technologies*.toUpperCase() // 相当于 technologies.collect { it?.toUpperCase() }
//Safe navigation operator: used to avoid a NullPointerException. //安全导航safe navigation运算符用来避免NullPointerException
def user = User.get(1) def user = User.get(1)
def username = user?.username def username = user?.username
/* /*
Closures 闭包
A Groovy Closure is like a "code block" or a method pointer. It is a piece of Groovy闭包好比代码块或者方法指针它是一段定义稍后执行的代码。
code that is defined and then executed at a later point.
More info at: http://www.groovy-lang.org/closures.html 更多信息见:http://www.groovy-lang.org/closures.html
*/ */
//Example: //例子:
def clos = { println "Hello World!" } def clos = { println "Hello World!" }
println "Executing the Closure:" println "Executing the Closure:"
clos() clos()
//Passing parameters to a closure //传参数给闭包
def sum = { a, b -> println a+b } def sum = { a, b -> println a+b }
sum(2,4) sum(2,4)
//Closures may refer to variables not listed in their parameter list. //闭包可以引用参数列表以外的变量
def x = 5 def x = 5
def multiplyBy = { num -> num * x } def multiplyBy = { num -> num * x }
println multiplyBy(10) println multiplyBy(10)
// If you have a Closure that takes a single argument, you may omit the // 只有一个参数的闭包可以省略参数的定义
// parameter definition of the Closure
def clos = { print it } def clos = { print it }
clos( "hi" ) clos( "hi" )
/* /*
Groovy can memorize closure results [1][2][3] Groovy可以记忆闭包结果 [1][2][3]
*/ */
def cl = {a, b -> def cl = {a, b ->
sleep(3000) // simulate some time consuming processing sleep(3000) // 模拟费时操作
a + b a + b
} }
@ -300,8 +295,7 @@ callClosure(3, 4)
/* /*
Expando Expando
The Expando class is a dynamic bean so we can add properties and we can add Expando类是一种动态bean类可以给它的实例添加属性和添加闭包作为方法
closures as methods to an instance of this class
http://mrhaki.blogspot.mx/2009/10/groovy-goodness-expando-as-dynamic-bean.html http://mrhaki.blogspot.mx/2009/10/groovy-goodness-expando-as-dynamic-bean.html
*/ */
@ -321,10 +315,10 @@ callClosure(3, 4)
/* /*
Metaprogramming (MOP) 元编程(MOP)
*/ */
//Using ExpandoMetaClass to add behaviour //使用ExpandoMetaClass增加行为
String.metaClass.testAdd = { String.metaClass.testAdd = {
println "we added this" println "we added this"
} }
@ -332,7 +326,7 @@ String.metaClass.testAdd = {
String x = "test" String x = "test"
x?.testAdd() x?.testAdd()
//Intercepting method calls //方法调用注入
class Test implements GroovyInterceptable { class Test implements GroovyInterceptable {
def sum(Integer x, Integer y) { x + y } def sum(Integer x, Integer y) { x + y }
@ -345,7 +339,7 @@ def test = new Test()
test?.sum(2,3) test?.sum(2,3)
test?.multiply(2,3) test?.multiply(2,3)
//Groovy supports propertyMissing for dealing with property resolution attempts. //Groovy支持propertyMissing来处理属性解析尝试
class Foo { class Foo {
def propertyMissing(String name) { name } def propertyMissing(String name) { name }
} }
@ -354,13 +348,12 @@ def f = new Foo()
assertEquals "boo", f.boo assertEquals "boo", f.boo
/* /*
TypeChecked and CompileStatic 类型检查和静态编译
Groovy, by nature, is and will always be a dynamic language but it supports Groovy天生是并将永远是一门静态语言但也支持类型检查和静态编译
typechecked and compilestatic
More info: http://www.infoq.com/articles/new-groovy-20 更多: http://www.infoq.com/articles/new-groovy-20
*/ */
//TypeChecked //类型检查
import groovy.transform.TypeChecked import groovy.transform.TypeChecked
void testMethod() {} void testMethod() {}
@ -375,7 +368,7 @@ void test() {
} }
//Another example: //另一例子
import groovy.transform.TypeChecked import groovy.transform.TypeChecked
@TypeChecked @TypeChecked
@ -390,7 +383,7 @@ Integer test() {
} }
//CompileStatic example: //静态编译例子
import groovy.transform.CompileStatic import groovy.transform.CompileStatic
@CompileStatic @CompileStatic
@ -403,15 +396,15 @@ assert sum(2,5) == 7
``` ```
## Further resources ## 进阶资源
[Groovy documentation](http://www.groovy-lang.org/documentation.html) [Groovy文档](http://www.groovy-lang.org/documentation.html)
[Groovy web console](http://groovyconsole.appspot.com/) [Groovy web console](http://groovyconsole.appspot.com/)
Join a [Groovy user group](http://www.groovy-lang.org/usergroups.html) 加入[Groovy用户组](http://www.groovy-lang.org/usergroups.html)
## Books ## 图书
* [Groovy Goodness] (https://leanpub.com/groovy-goodness-notebook) * [Groovy Goodness] (https://leanpub.com/groovy-goodness-notebook)