From 18a45cf7a37eb971383e3fcf65a565292e82224b Mon Sep 17 00:00:00 2001 From: Alwayswithme Date: Wed, 20 May 2015 00:14:10 +0800 Subject: update bash-cn --- zh-cn/bash-cn.html.markdown | 147 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 139 insertions(+), 8 deletions(-) (limited to 'zh-cn') diff --git a/zh-cn/bash-cn.html.markdown b/zh-cn/bash-cn.html.markdown index 6afa659a..13f85bb7 100644 --- a/zh-cn/bash-cn.html.markdown +++ b/zh-cn/bash-cn.html.markdown @@ -5,7 +5,14 @@ contributors: - ["Max Yankov", "https://github.com/golergka"] - ["Darren Lin", "https://github.com/CogBear"] - ["Alexandre Medeiros", "http://alemedeiros.sdf.org"] + - ["Denis Arh", "https://github.com/darh"] + - ["akirahirose", "https://twitter.com/akirahirose"] + - ["Anton Strömkvist", "http://lutic.org/"] + - ["Rahil Momin", "https://github.com/iamrahil"] + - ["Gregrory Kielian", "https://github.com/gskielian"] + - ["Etan Reisner", "https://github.com/deryni"] translators: + - ["Jinchang Ye", "https://github.com/Alwayswithme"] - ["Chunyang Xu", "https://github.com/XuChunyang"] filename: LearnBash-cn.sh lang: zh-cn @@ -35,6 +42,10 @@ VARIABLE="Some string" VARIABLE = "Some string" # Bash 会把 VARIABLE 当做一个指令,由于找不到该指令,因此这里会报错。 +# 也不可以这样: +Variable= 'Some string' +# Bash 会认为 'Some string' 是一条指令,由于找不到该指令,这里再次报错。 +# (这个例子中 'Variable=' 这部分的赋值仅对 'Some string' 指令起作用。) # 使用变量: echo $VARIABLE @@ -49,6 +60,16 @@ echo '$VARIABLE' echo ${VARIABLE/Some/A} # 会把 VARIABLE 中首次出现的 "some" 替换成 “A”。 +# 变量的截取 +Length=7 +echo ${Variable:0:Length} +# 这样会仅返回变量值的前7个字符 + +# 变量的默认值 +echo ${Foo:-"DefaultValueIfFooIsMissingOrEmpty"} +# 对 null (Foo=) 和空串 (Foo="") 起作用; 零(Foo=0)时返回0 +# 注意这仅返回默认值而不是改变变量的值 + # 内置变量: # 下面的内置变量很有用 echo "Last program return value: $?" @@ -75,6 +96,17 @@ fi echo "Always executed" || echo "Only executed if first command fail" echo "Always executed" && echo "Only executed if first command does NOT fail" +# 在 if 语句中使用 && 和 || 需要多对方括号 +if [ $Name == "Steve" ] && [ $Age -eq 15 ] +then + echo "This will run if $Name is Steve AND $Age is 15." +fi + +if [ $Name == "Daniya" ] || [ $Name == "Zach" ] +then + echo "This will run if $Name is Daniya OR Zach." +fi + # 表达式的格式如下: echo $(( 10 + 5 )) @@ -88,16 +120,52 @@ ls -l # 列出文件和目录的详细信息 # 用下面的指令列出当前目录下所有的 txt 文件: ls -l | grep "\.txt" +# 重定向输入和输出(标准输入,标准输出,标准错误)。 +# 以 ^EOF$ 作为结束标记从标准输入读取数据并覆盖 hello.py : +cat > hello.py << EOF +#!/usr/bin/env python +from __future__ import print_function +import sys +print("#stdout", file=sys.stdout) +print("#stderr", file=sys.stderr) +for line in sys.stdin: + print(line, file=sys.stdout) +EOF + # 重定向可以到输出,输入和错误输出。 -python2 hello.py < "input.in" -python2 hello.py > "output.out" -python2 hello.py 2> "error.err" +python hello.py < "input.in" +python hello.py > "output.out" +python hello.py 2> "error.err" +python hello.py > "output-and-error.log" 2>&1 +python hello.py > /dev/null 2>&1 # > 会覆盖已存在的文件, >> 会以累加的方式输出文件中。 +python hello.py >> "output.out" 2>> "error.err" + +# 覆盖 output.out , 追加 error.err 并统计行数 +info bash 'Basic Shell Features' 'Redirections' > output.out 2>> error.err +wc -l output.out error.err + +# 运行指令并打印文件描述符 (比如 /dev/fd/123) +# 具体可查看: man fd +echo <(echo "#helloworld") + +# 以 "#helloworld" 覆盖 output.out: +cat > output.out <(echo "#helloworld") +echo "#helloworld" > output.out +echo "#helloworld" | cat > output.out +echo "#helloworld" | tee output.out >/dev/null + +# 清理临时文件并显示详情(增加 '-i' 选项启用交互模式) +rm -v output.out error.err output-and-error.log # 一个指令可用 $( ) 嵌套在另一个指令内部: # 以下的指令会打印当前目录下的目录和文件总数 echo "There are $(ls | wc -l) items here." +# 反引号 `` 起相同作用,但不允许嵌套 +# 优先使用 $( ). +echo "There are `ls | wc -l` items here." + # Bash 的 case 语句与 Java 和 C++ 中的 switch 语句类似: case "$VARIABLE" in # 列出需要匹配的字符串 @@ -114,6 +182,33 @@ do echo "$VARIABLE" done +# 或传统的 “for循环” : +for ((a=1; a <= 3; a++)) +do + echo $a +done + +# 也可以用于文件 +# 用 cat 输出 file1 和 file2 内容 +for Variable in file1 file2 +do + cat "$Variable" +done + +# 或作用于其他命令的输出 +# 对 ls 输出的文件执行 cat 指令。 +for Output in $(ls) +do + cat "$Output" +done + +# while 循环: +while [ true ] +do + echo "loop body here..." + break +done + # 你也可以使用函数 # 定义函数: function foo () @@ -135,14 +230,50 @@ bar () foo "My name is" $NAME # 有很多有用的指令需要学习: -tail -n 10 file.txt # 打印 file.txt 的最后 10 行 -head -n 10 file.txt +tail -n 10 file.txt # 打印 file.txt 的前 10 行 -sort file.txt +head -n 10 file.txt # 将 file.txt 按行排序 -uniq -d file.txt +sort file.txt # 报告或忽略重复的行,用选项 -d 打印重复的行 -cut -d ',' -f 1 file.txt +uniq -d file.txt # 打印每行中 ',' 之前内容 +cut -d ',' -f 1 file.txt +# 将 file.txt 文件所有 'okay' 替换为 'great', (兼容正则表达式) +sed -i 's/okay/great/g' file.txt +# 将 file.txt 中匹配正则的行打印到标准输出 +# 这里打印以 "foo" 开头, "bar" 结尾的行 +grep "^foo.*bar$" file.txt +# 使用选项 "-c" 统计行数 +grep -c "^foo.*bar$" file.txt +# 如果只是要按字面形式搜索字符串而不是按正则表达式,使用 fgrep (或 grep -F) +fgrep "^foo.*bar$" file.txt + + +# 以 bash 内建的 'help' 指令阅读 Bash 自带文档: +help +help help +help for +help return +help source +help . + +# 用 mam 指令阅读相关的 Bash 手册 +apropos bash +man 1 bash +man bash + +# 用 info 指令查阅命令的 info 文档 (info 中输入 ? 显示帮助信息) +apropos info | grep '^info.*(' +man info +info info +info 5 info + +# 阅读 Bash 的 info 文档: +info bash +info bash 'Bash Features' +info bash 6 +info --apropos bash + ``` -- cgit v1.2.3 From 7a4538374c4b86a355573039cc5426a32e936d11 Mon Sep 17 00:00:00 2001 From: ryan ouyang Date: Thu, 28 May 2015 13:07:01 +0800 Subject: Update type error of chinese translation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Line 344: From "行数" to "函数" --- zh-cn/javascript-cn.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'zh-cn') diff --git a/zh-cn/javascript-cn.html.markdown b/zh-cn/javascript-cn.html.markdown index 64b0aadc..b450ab84 100644 --- a/zh-cn/javascript-cn.html.markdown +++ b/zh-cn/javascript-cn.html.markdown @@ -341,7 +341,7 @@ var myFunc = myObj.myFunc; myFunc(); // = undefined // 相应的,一个函数也可以被指定为一个对象的方法,并且可以通过`this`访问 -// 这个对象的成员,即使在行数被定义时并没有依附在对象上。 +// 这个对象的成员,即使在函数被定义时并没有依附在对象上。 var myOtherFunc = function(){ return this.myString.toUpperCase(); } -- cgit v1.2.3 From 59b719015a5fadf6a1d12854a19b0196c8481ec8 Mon Sep 17 00:00:00 2001 From: Alwayswithme Date: Tue, 2 Jun 2015 15:55:12 +0800 Subject: minor revision --- zh-cn/bash-cn.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'zh-cn') diff --git a/zh-cn/bash-cn.html.markdown b/zh-cn/bash-cn.html.markdown index 13f85bb7..63bec888 100644 --- a/zh-cn/bash-cn.html.markdown +++ b/zh-cn/bash-cn.html.markdown @@ -45,7 +45,7 @@ VARIABLE = "Some string" # 也不可以这样: Variable= 'Some string' # Bash 会认为 'Some string' 是一条指令,由于找不到该指令,这里再次报错。 -# (这个例子中 'Variable=' 这部分的赋值仅对 'Some string' 指令起作用。) +# (这个例子中 'Variable=' 这部分会被当作仅对 'Some string' 起作用的赋值。) # 使用变量: echo $VARIABLE @@ -264,7 +264,7 @@ apropos bash man 1 bash man bash -# 用 info 指令查阅命令的 info 文档 (info 中输入 ? 显示帮助信息) +# 用 info 指令查阅命令的 info 文档 (info 中按 ? 显示帮助信息) apropos info | grep '^info.*(' man info info info -- cgit v1.2.3 From da0d1819f85489990418713f565013f51811bdb9 Mon Sep 17 00:00:00 2001 From: Alwayswithme Date: Tue, 2 Jun 2015 16:38:11 +0800 Subject: only capitalize first character of the variable --- zh-cn/bash-cn.html.markdown | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'zh-cn') diff --git a/zh-cn/bash-cn.html.markdown b/zh-cn/bash-cn.html.markdown index 63bec888..1b79f326 100644 --- a/zh-cn/bash-cn.html.markdown +++ b/zh-cn/bash-cn.html.markdown @@ -36,11 +36,11 @@ echo Hello, world! echo 'This is the first line'; echo 'This is the second line' # 声明一个变量: -VARIABLE="Some string" +Variable="Some string" # 下面是错误的做法: -VARIABLE = "Some string" -# Bash 会把 VARIABLE 当做一个指令,由于找不到该指令,因此这里会报错。 +Variable = "Some string" +# Bash 会把 Variable 当做一个指令,由于找不到该指令,因此这里会报错。 # 也不可以这样: Variable= 'Some string' @@ -48,17 +48,17 @@ Variable= 'Some string' # (这个例子中 'Variable=' 这部分会被当作仅对 'Some string' 起作用的赋值。) # 使用变量: -echo $VARIABLE -echo "$VARIABLE" -echo '$VARIABLE' +echo $Variable +echo "$Variable" +echo '$Variable' # 当你赋值 (assign) 、导出 (export),或者以其他方式使用变量时,变量名前不加 $。 # 如果要使用变量的值, 则要加 $。 # 注意: ' (单引号) 不会展开变量(即会屏蔽掉变量)。 # 在变量内部进行字符串代换 -echo ${VARIABLE/Some/A} -# 会把 VARIABLE 中首次出现的 "some" 替换成 “A”。 +echo ${Variable/Some/A} +# 会把 Variable 中首次出现的 "some" 替换成 “A”。 # 变量的截取 Length=7 @@ -80,12 +80,12 @@ echo "Scripts arguments separeted in different variables: $1 $2..." # 读取输入: echo "What's your name?" -read NAME # 这里不需要声明新变量 -echo Hello, $NAME! +read Name # 这里不需要声明新变量 +echo Hello, $Name! # 通常的 if 结构看起来像这样: # 'man test' 可查看更多的信息 -if [ $NAME -ne $USER ] +if [ $Name -ne $USER ] then echo "Your name is you username" else @@ -167,7 +167,7 @@ echo "There are $(ls | wc -l) items here." echo "There are `ls | wc -l` items here." # Bash 的 case 语句与 Java 和 C++ 中的 switch 语句类似: -case "$VARIABLE" in +case "$Variable" in # 列出需要匹配的字符串 0) echo "There is a zero.";; 1) echo "There is a one.";; @@ -175,11 +175,11 @@ case "$VARIABLE" in esac # 循环遍历给定的参数序列: -# 变量$VARIABLE 的值会被打印 3 次。 +# 变量$Variable 的值会被打印 3 次。 # 注意 ` ` 和 $( ) 等价。seq 返回长度为 3 的数组。 -for VARIABLE in `seq 3` +for Variable in `seq 3` do - echo "$VARIABLE" + echo "$Variable" done # 或传统的 “for循环” : @@ -227,7 +227,7 @@ bar () } # 调用函数 -foo "My name is" $NAME +foo "My name is" $Name # 有很多有用的指令需要学习: # 打印 file.txt 的最后 10 行 -- cgit v1.2.3 From ee7f99fc461afad7d4aba4ac4226bdf7102abee4 Mon Sep 17 00:00:00 2001 From: Alwayswithme Date: Tue, 2 Jun 2015 17:02:23 +0800 Subject: command sync with english version --- zh-cn/bash-cn.html.markdown | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'zh-cn') diff --git a/zh-cn/bash-cn.html.markdown b/zh-cn/bash-cn.html.markdown index 1b79f326..558d9110 100644 --- a/zh-cn/bash-cn.html.markdown +++ b/zh-cn/bash-cn.html.markdown @@ -30,7 +30,7 @@ Bash 是一个为 GNU 计划编写的 Unix shell,是 Linux 和 Mac OS X 下的 # 如你所见,注释以 # 开头,shebang 也是注释。 # 显示 “Hello world!” -echo Hello, world! +echo Hello world! # 每一句指令以换行或分号隔开: echo 'This is the first line'; echo 'This is the second line' @@ -76,7 +76,7 @@ echo "Last program return value: $?" echo "Script's PID: $$" echo "Number of arguments: $#" echo "Scripts arguments: $@" -echo "Scripts arguments separeted in different variables: $1 $2..." +echo "Scripts arguments separated in different variables: $1 $2..." # 读取输入: echo "What's your name?" @@ -87,13 +87,13 @@ echo Hello, $Name! # 'man test' 可查看更多的信息 if [ $Name -ne $USER ] then - echo "Your name is you username" + echo "Your name isn't your username" else - echo "Your name isn't you username" + echo "Your name is your username" fi # 根据上一个指令执行结果决定是否执行下一个指令 -echo "Always executed" || echo "Only executed if first command fail" +echo "Always executed" || echo "Only executed if first command fails" echo "Always executed" && echo "Only executed if first command does NOT fail" # 在 if 语句中使用 && 和 || 需要多对方括号 @@ -176,8 +176,7 @@ esac # 循环遍历给定的参数序列: # 变量$Variable 的值会被打印 3 次。 -# 注意 ` ` 和 $( ) 等价。seq 返回长度为 3 的数组。 -for Variable in `seq 3` +for Variable in {1..3} do echo "$Variable" done @@ -275,5 +274,4 @@ info bash info bash 'Bash Features' info bash 6 info --apropos bash - ``` -- cgit v1.2.3 From 6b109e1460e11cb1030f4832ee67e89283cc6808 Mon Sep 17 00:00:00 2001 From: Todd Gao Date: Thu, 18 Jun 2015 22:20:30 +0800 Subject: cp original file to zh-cn/ --- zh-cn/groovy-cn.html.markdown | 427 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 427 insertions(+) create mode 100644 zh-cn/groovy-cn.html.markdown (limited to 'zh-cn') diff --git a/zh-cn/groovy-cn.html.markdown b/zh-cn/groovy-cn.html.markdown new file mode 100644 index 00000000..519f36ce --- /dev/null +++ b/zh-cn/groovy-cn.html.markdown @@ -0,0 +1,427 @@ +--- +language: Groovy +filename: learngroovy.groovy +contributors: + - ["Roberto Pérez Alcolea", "http://github.com/rpalcolea"] +filename: learngroovy.groovy +--- + +Groovy - A dynamic language for the Java platform [Read more here.](http://www.groovy-lang.org/) + +```groovy + +/* + Set yourself up: + + 1) Install GVM - http://gvmtool.net/ + 2) Install Groovy: gvm install groovy + 3) Start the groovy console by typing: groovyConsole + +*/ + +// Single line comments start with two forward slashes +/* +Multi line comments look like this. +*/ + +// Hello World +println "Hello world!" + +/* + Variables: + + You can assign values to variables for later use +*/ + +def x = 1 +println x + +x = new java.util.Date() +println x + +x = -3.1499392 +println x + +x = false +println x + +x = "Groovy!" +println x + +/* + Collections and maps +*/ + +//Creating an empty list +def technologies = [] + +/*** Adding a elements to the list ***/ + +// As with Java +technologies.add("Grails") + +// Left shift adds, and returns the list +technologies << "Groovy" + +// Add multiple elements +technologies.addAll(["Gradle","Griffon"]) + +/*** Removing elements from the list ***/ + +// As with Java +technologies.remove("Griffon") + +// Subtraction works also +technologies = technologies - 'Grails' + +/*** Iterating Lists ***/ + +// Iterate over elements of a list +technologies.each { println "Technology: $it"} +technologies.eachWithIndex { it, i -> println "$i: $it"} + +/*** Checking List contents ***/ + +//Evaluate if a list contains element(s) (boolean) +contained = technologies.contains( 'Groovy' ) + +// Or +contained = 'Groovy' in technologies + +// Check for multiple contents +technologies.containsAll(['Groovy','Grails']) + +/*** Sorting Lists ***/ + +// Sort a list (mutates original list) +technologies.sort() + +// To sort without mutating original, you can do: +sortedTechnologies = technologies.sort( false ) + +/*** Manipulating Lists ***/ + +//Replace all elements in the list +Collections.replaceAll(technologies, 'Gradle', 'gradle') + +//Shuffle a list +Collections.shuffle(technologies, new Random()) + +//Clear a list +technologies.clear() + +//Creating an empty map +def devMap = [:] + +//Add values +devMap = ['name':'Roberto', 'framework':'Grails', 'language':'Groovy'] +devMap.put('lastName','Perez') + +//Iterate over elements of a map +devMap.each { println "$it.key: $it.value" } +devMap.eachWithIndex { it, i -> println "$i: $it"} + +//Evaluate if a map contains a key +assert devMap.containsKey('name') + +//Evaluate if a map contains a value +assert devMap.containsValue('Roberto') + +//Get the keys of a map +println devMap.keySet() + +//Get the values of a map +println devMap.values() + +/* + Groovy Beans + + GroovyBeans are JavaBeans but using a much simpler syntax + + When Groovy is compiled to bytecode, the following rules are used. + + * If the name is declared with an access modifier (public, private or + protected) then a field is generated. + + * A name declared with no access modifier generates a private field with + public getter and setter (i.e. a property). + + * If a property is declared final the private field is created final and no + setter is generated. + + * You can declare a property and also declare your own getter or 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 + getter and setter which must be declared private or protected. + + * If you access a property from within the class the property is defined in + at compile time with implicit or explicit this (for example this.foo, or + 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 + implicit foo, then Groovy will access the property through the meta class, + which may fail at runtime. + +*/ + +class Foo { + // read only property + final String name = "Roberto" + + // read only property with public getter and protected setter + String language + protected void setLanguage(String language) { this.language = language } + + // dynamically typed property + def lastName +} + +/* + Logical Branching and Looping +*/ + +//Groovy supports the usual if - else syntax +def x = 3 + +if(x==1) { + println "One" +} else if(x==2) { + println "Two" +} else { + println "X greater than Two" +} + +//Groovy also supports the ternary operator: +def y = 10 +def x = (y > 1) ? "worked" : "failed" +assert x == "worked" + +//For loop +//Iterate over a range +def x = 0 +for (i in 0 .. 30) { + x += i +} + +//Iterate over a list +x = 0 +for( i in [5,3,2,1] ) { + x += i +} + +//Iterate over an array +array = (0..20).toArray() +x = 0 +for (i in array) { + x += i +} + +//Iterate over a map +def map = ['name':'Roberto', 'framework':'Grails', 'language':'Groovy'] +x = 0 +for ( e in map ) { + x += e.value +} + +/* + Operators + + Operator Overloading for a list of the common operators that Groovy supports: + http://www.groovy-lang.org/operators.html#Operator-Overloading + + Helpful groovy operators +*/ +//Spread operator: invoke an action on all items of an aggregate object. +def technologies = ['Groovy','Grails','Gradle'] +technologies*.toUpperCase() // = to technologies.collect { it?.toUpperCase() } + +//Safe navigation operator: used to avoid a NullPointerException. +def user = User.get(1) +def username = user?.username + + +/* + Closures + A Groovy Closure is like a "code block" or a method pointer. It is a piece of + code that is defined and then executed at a later point. + + More info at: http://www.groovy-lang.org/closures.html +*/ +//Example: +def clos = { println "Hello World!" } + +println "Executing the Closure:" +clos() + +//Passing parameters to a closure +def sum = { a, b -> println a+b } +sum(2,4) + +//Closures may refer to variables not listed in their parameter list. +def x = 5 +def multiplyBy = { num -> num * x } +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 } +clos( "hi" ) + +/* + Groovy can memorize closure results [1][2][3] +*/ +def cl = {a, b -> + sleep(3000) // simulate some time consuming processing + a + b +} + +mem = cl.memoize() + +def callClosure(a, b) { + def start = System.currentTimeMillis() + mem(a, b) + println "Inputs(a = $a, b = $b) - took ${System.currentTimeMillis() - start} msecs." +} + +callClosure(1, 2) +callClosure(1, 2) +callClosure(2, 3) +callClosure(2, 3) +callClosure(3, 4) +callClosure(3, 4) +callClosure(1, 2) +callClosure(2, 3) +callClosure(3, 4) + +/* + Expando + + The Expando class is a dynamic bean so we can add properties and we can add + closures as methods to an instance of this class + + http://mrhaki.blogspot.mx/2009/10/groovy-goodness-expando-as-dynamic-bean.html +*/ + def user = new Expando(name:"Roberto") + assert 'Roberto' == user.name + + user.lastName = 'Pérez' + assert 'Pérez' == user.lastName + + user.showInfo = { out -> + out << "Name: $name" + out << ", Last name: $lastName" + } + + def sw = new StringWriter() + println user.showInfo(sw) + + +/* + Metaprogramming (MOP) +*/ + +//Using ExpandoMetaClass to add behaviour +String.metaClass.testAdd = { + println "we added this" +} + +String x = "test" +x?.testAdd() + +//Intercepting method calls +class Test implements GroovyInterceptable { + def sum(Integer x, Integer y) { x + y } + + def invokeMethod(String name, args) { + System.out.println "Invoke method $name with args: $args" + } +} + +def test = new Test() +test?.sum(2,3) +test?.multiply(2,3) + +//Groovy supports propertyMissing for dealing with property resolution attempts. +class Foo { + def propertyMissing(String name) { name } +} +def f = new Foo() + +assertEquals "boo", f.boo + +/* + TypeChecked and CompileStatic + Groovy, by nature, is and will always be a dynamic language but it supports + typechecked and compilestatic + + More info: http://www.infoq.com/articles/new-groovy-20 +*/ +//TypeChecked +import groovy.transform.TypeChecked + +void testMethod() {} + +@TypeChecked +void test() { + testMeethod() + + def name = "Roberto" + + println naameee + +} + +//Another example: +import groovy.transform.TypeChecked + +@TypeChecked +Integer test() { + Integer num = "1" + + Integer[] numbers = [1,2,3,4] + + Date date = numbers[1] + + return "Test" + +} + +//CompileStatic example: +import groovy.transform.CompileStatic + +@CompileStatic +int sum(int x, int y) { + x + y +} + +assert sum(2,5) == 7 + + +``` + +## Further resources + +[Groovy documentation](http://www.groovy-lang.org/documentation.html) + +[Groovy web console](http://groovyconsole.appspot.com/) + +Join a [Groovy user group](http://www.groovy-lang.org/usergroups.html) + +## Books + +* [Groovy Goodness] (https://leanpub.com/groovy-goodness-notebook) + +* [Groovy in Action] (http://manning.com/koenig2/) + +* [Programming Groovy 2: Dynamic Productivity for the Java Developer] (http://shop.oreilly.com/product/9781937785307.do) + +[1] http://roshandawrani.wordpress.com/2010/10/18/groovy-new-feature-closures-can-now-memorize-their-results/ +[2] http://www.solutionsiq.com/resources/agileiq-blog/bid/72880/Programming-with-Groovy-Trampoline-and-Memoize +[3] http://mrhaki.blogspot.mx/2011/05/groovy-goodness-cache-closure-results.html + + + -- cgit v1.2.3 From 021c80723e3b28d81c852b6fd58b6ec9e34670ff Mon Sep 17 00:00:00 2001 From: Todd Gao Date: Fri, 19 Jun 2015 10:58:39 +0800 Subject: zh-cn translation for groovy --- zh-cn/groovy-cn.html.markdown | 203 ++++++++++++++++++++---------------------- 1 file changed, 98 insertions(+), 105 deletions(-) (limited to 'zh-cn') diff --git a/zh-cn/groovy-cn.html.markdown b/zh-cn/groovy-cn.html.markdown index 519f36ce..ccd48a4c 100644 --- a/zh-cn/groovy-cn.html.markdown +++ b/zh-cn/groovy-cn.html.markdown @@ -1,36 +1,38 @@ --- language: Groovy -filename: learngroovy.groovy +filename: learngroovy-cn.groovy contributors: - ["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 /* - Set yourself up: + 安装: - 1) Install GVM - http://gvmtool.net/ - 2) Install Groovy: gvm install groovy - 3) Start the groovy console by typing: groovyConsole + 1) 安装 GVM - http://gvmtool.net/ + 2) 安装 Groovy: gvm install groovy + 3) 启动 groovy 控制台,键入: groovyConsole */ -// Single line comments start with two forward slashes +// 双斜线开始的是单行注释 /* -Multi line comments look like this. +像这样的是多行注释 */ // Hello World println "Hello world!" /* - Variables: + 变量: - You can assign values to variables for later use + 可以给变量赋值,稍后再用 */ def x = 1 @@ -49,142 +51,137 @@ x = "Groovy!" println x /* - Collections and maps + 集合和map */ -//Creating an empty list +//创建一个空的列表 def technologies = [] -/*** Adding a elements to the list ***/ +/*** 往列表中增加一个元素 ***/ -// As with Java +// 和Java一样 technologies.add("Grails") -// Left shift adds, and returns the list +// 左移添加,返回该列表 technologies << "Groovy" -// Add multiple elements +// 增加多个元素 technologies.addAll(["Gradle","Griffon"]) -/*** Removing elements from the list ***/ +/*** 从列表中删除元素 ***/ -// As with Java +// 和Java一样 technologies.remove("Griffon") -// Subtraction works also +// 减法也行 technologies = technologies - 'Grails' -/*** Iterating Lists ***/ +/*** 遍历列表 ***/ -// Iterate over elements of a list +// 遍历列表中的元素 technologies.each { println "Technology: $it"} technologies.eachWithIndex { it, i -> println "$i: $it"} -/*** Checking List contents ***/ +/*** 检查列表内容 ***/ -//Evaluate if a list contains element(s) (boolean) +//判断列表是否包含某元素,返回boolean contained = technologies.contains( 'Groovy' ) -// Or +// 或 contained = 'Groovy' in technologies -// Check for multiple contents +// 检查多个元素 technologies.containsAll(['Groovy','Grails']) -/*** Sorting Lists ***/ +/*** 排序列表 ***/ -// Sort a list (mutates original list) +// 排序列表(修改原列表) technologies.sort() -// To sort without mutating original, you can do: +// 要想不修改原列表,可以这样: sortedTechnologies = technologies.sort( false ) -/*** Manipulating Lists ***/ +/*** 操作列表 ***/ -//Replace all elements in the list +//替换列表元素 Collections.replaceAll(technologies, 'Gradle', 'gradle') -//Shuffle a list +//打乱列表 Collections.shuffle(technologies, new Random()) -//Clear a list +//清空列表 technologies.clear() -//Creating an empty map +//创建空的map def devMap = [:] -//Add values +//增加值 devMap = ['name':'Roberto', 'framework':'Grails', 'language':'Groovy'] devMap.put('lastName','Perez') -//Iterate over elements of a map +//遍历map元素 devMap.each { println "$it.key: $it.value" } devMap.eachWithIndex { it, i -> println "$i: $it"} -//Evaluate if a map contains a key +//判断map是否包含某键 assert devMap.containsKey('name') -//Evaluate if a map contains a value +//判断map是否包含某值 assert devMap.containsValue('Roberto') -//Get the keys of a map +//取得map所有的键 println devMap.keySet() -//Get the values of a map +//取得map所有的值 println devMap.values() /* 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 - protected) then a field is generated. + * 如果一个名字声明时带有访问修饰符(public, private, 或者 protected), + 则会生成一个字段(field)。 - * A name declared with no access modifier generates a private field with - public getter and setter (i.e. a property). + * 名字声明时没有访问修饰符,则会生成一个带有public getter和setter的 + private字段,即属性(property)。 - * If a property is declared final the private field is created final and no - setter is generated. + * 如果一个属性声明为final,则会创建一个final的private字段,但不会生成setter。 - * 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 - getter and setter which must be declared private or protected. + * 如果要定义private或protected属性,必须提供声明为private或protected的getter + 和setter。 - * If you access a property from within the class the property is defined in - at compile time with implicit or explicit this (for example this.foo, or - simply foo), Groovy will access the field directly instead of going though - the getter and setter. + * 如果使用显式或隐式的 this(例如 this.foo, 或者 foo)访问类的在编译时定义的属性, + Groovy会直接访问对应字段,而不是使用getter或者setter - * If you access a property that does not exist using the explicit or - implicit foo, then Groovy will access the property through the meta class, - which may fail at runtime. + * 如果使用显式或隐式的 foo 访问一个不存在的属性,Groovy会通过元类(meta class) + 访问它,这可能导致运行时错误。 */ class Foo { - // read only property + // 只读属性 final String name = "Roberto" - // read only property with public getter and protected setter + // 只读属性,有public getter和protected setter String language protected void setLanguage(String language) { this.language = language } - // dynamically typed property + // 动态类型属性 def lastName } /* - Logical Branching and Looping + 逻辑分支和循环 */ -//Groovy supports the usual if - else syntax +//Groovy支持常见的if - else语法 def x = 3 if(x==1) { @@ -195,32 +192,32 @@ if(x==1) { println "X greater than Two" } -//Groovy also supports the ternary operator: +//Groovy也支持三元运算符 def y = 10 def x = (y > 1) ? "worked" : "failed" assert x == "worked" -//For loop -//Iterate over a range +//for循环 +//使用区间(range)遍历 def x = 0 for (i in 0 .. 30) { x += i } -//Iterate over a list +//遍历列表 x = 0 for( i in [5,3,2,1] ) { x += i } -//Iterate over an array +//遍历数组 array = (0..20).toArray() x = 0 for (i in array) { x += i } -//Iterate over a map +//遍历map def map = ['name':'Roberto', 'framework':'Grails', 'language':'Groovy'] x = 0 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 - Helpful groovy operators + 实用的groovy运算符 */ -//Spread operator: invoke an action on all items of an aggregate object. +//展开(spread)运算符:对聚合对象的所有元素施加操作 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 username = user?.username /* - Closures - A Groovy Closure is like a "code block" or a method pointer. It is a piece of - code that is defined and then executed at a later point. + 闭包 + Groovy闭包好比代码块或者方法指针,它是一段定义稍后执行的代码。 - More info at: http://www.groovy-lang.org/closures.html + 更多信息见:http://www.groovy-lang.org/closures.html */ -//Example: +//例子: def clos = { println "Hello World!" } println "Executing the Closure:" clos() -//Passing parameters to a closure +//传参数给闭包 def sum = { a, b -> println a+b } sum(2,4) -//Closures may refer to variables not listed in their parameter list. +//闭包可以引用参数列表以外的变量 def x = 5 def multiplyBy = { num -> num * x } 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 } clos( "hi" ) /* - Groovy can memorize closure results [1][2][3] + Groovy可以记忆闭包结果 [1][2][3] */ def cl = {a, b -> - sleep(3000) // simulate some time consuming processing + sleep(3000) // 模拟费时操作 a + b } @@ -300,8 +295,7 @@ callClosure(3, 4) /* Expando - The Expando class is a dynamic bean so we can add properties and we can add - closures as methods to an instance of this class + Expando类是一种动态bean类,可以给它的实例添加属性和添加闭包作为方法 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 = { println "we added this" } @@ -332,7 +326,7 @@ String.metaClass.testAdd = { String x = "test" x?.testAdd() -//Intercepting method calls +//方法调用注入 class Test implements GroovyInterceptable { def sum(Integer x, Integer y) { x + y } @@ -345,7 +339,7 @@ def test = new Test() test?.sum(2,3) test?.multiply(2,3) -//Groovy supports propertyMissing for dealing with property resolution attempts. +//Groovy支持propertyMissing,来处理属性解析尝试 class Foo { def propertyMissing(String name) { name } } @@ -354,13 +348,12 @@ def f = new Foo() assertEquals "boo", f.boo /* - TypeChecked and CompileStatic - Groovy, by nature, is and will always be a dynamic language but it supports - typechecked and compilestatic + 类型检查和静态编译 + Groovy天生是并将永远是一门静态语言,但也支持类型检查和静态编译 - More info: http://www.infoq.com/articles/new-groovy-20 + 更多: http://www.infoq.com/articles/new-groovy-20 */ -//TypeChecked +//类型检查 import groovy.transform.TypeChecked void testMethod() {} @@ -375,7 +368,7 @@ void test() { } -//Another example: +//另一例子 import groovy.transform.TypeChecked @TypeChecked @@ -390,7 +383,7 @@ Integer test() { } -//CompileStatic example: +//静态编译例子 import groovy.transform.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/) -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) -- cgit v1.2.3 From a6a498efab8b3ed2e8062a97e0fd47fb181b98f4 Mon Sep 17 00:00:00 2001 From: Todd Gao Date: Fri, 19 Jun 2015 12:16:41 +0800 Subject: update Chinese translation --- zh-cn/groovy-cn.html.markdown | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'zh-cn') diff --git a/zh-cn/groovy-cn.html.markdown b/zh-cn/groovy-cn.html.markdown index ccd48a4c..562a0284 100644 --- a/zh-cn/groovy-cn.html.markdown +++ b/zh-cn/groovy-cn.html.markdown @@ -32,7 +32,7 @@ println "Hello world!" /* 变量: - 可以给变量赋值,稍后再用 + 可以给变量赋值,以便稍后使用 */ def x = 1 @@ -51,7 +51,7 @@ x = "Groovy!" println x /* - 集合和map + 集合和映射 */ //创建一个空的列表 @@ -73,7 +73,7 @@ technologies.addAll(["Gradle","Griffon"]) // 和Java一样 technologies.remove("Griffon") -// 减法也行 +// 减号也行 technologies = technologies - 'Grails' /*** 遍历列表 ***/ @@ -93,7 +93,7 @@ contained = 'Groovy' in technologies // 检查多个元素 technologies.containsAll(['Groovy','Grails']) -/*** 排序列表 ***/ +/*** 列表排序 ***/ // 排序列表(修改原列表) technologies.sort() @@ -101,7 +101,7 @@ technologies.sort() // 要想不修改原列表,可以这样: sortedTechnologies = technologies.sort( false ) -/*** 操作列表 ***/ +/*** 列表操作 ***/ //替换列表元素 Collections.replaceAll(technologies, 'Gradle', 'gradle') @@ -112,27 +112,27 @@ Collections.shuffle(technologies, new Random()) //清空列表 technologies.clear() -//创建空的map +//创建空的映射 def devMap = [:] //增加值 devMap = ['name':'Roberto', 'framework':'Grails', 'language':'Groovy'] devMap.put('lastName','Perez') -//遍历map元素 +//遍历映射元素 devMap.each { println "$it.key: $it.value" } devMap.eachWithIndex { it, i -> println "$i: $it"} -//判断map是否包含某键 +//判断映射是否包含某键 assert devMap.containsKey('name') -//判断map是否包含某值 +//判断映射是否包含某值 assert devMap.containsValue('Roberto') -//取得map所有的键 +//取得映射所有的键 println devMap.keySet() -//取得map所有的值 +//取得映射所有的值 println devMap.values() /* @@ -217,7 +217,7 @@ for (i in array) { x += i } -//遍历map +//遍历映射 def map = ['name':'Roberto', 'framework':'Grails', 'language':'Groovy'] x = 0 for ( e in map ) { @@ -227,7 +227,7 @@ for ( e in map ) { /* 运算符 - Groovy中下列运算符支持重载: + 在Groovy中以下常用运算符支持重载: http://www.groovy-lang.org/operators.html#Operator-Overloading 实用的groovy运算符 @@ -243,7 +243,7 @@ def username = user?.username /* 闭包 - Groovy闭包好比代码块或者方法指针,它是一段定义稍后执行的代码。 + Groovy闭包好比代码块或者方法指针,它是一段代码定义,可以以后执行。 更多信息见:http://www.groovy-lang.org/closures.html */ @@ -326,7 +326,7 @@ String.metaClass.testAdd = { String x = "test" x?.testAdd() -//方法调用注入 +//拦截方法调用 class Test implements GroovyInterceptable { def sum(Integer x, Integer y) { x + y } @@ -349,7 +349,7 @@ assertEquals "boo", f.boo /* 类型检查和静态编译 - Groovy天生是并将永远是一门静态语言,但也支持类型检查和静态编译 + Groovy天生是并将永远是一门动态语言,但也支持类型检查和静态编译 更多: http://www.infoq.com/articles/new-groovy-20 */ -- cgit v1.2.3