曾有大师级的人物预言未来属于动态语言。Groovy就是java世界的动态语言。这篇文章只是groovy的一个热身。
1、安装JDK环境
Groovy需要JDK1.4以上版本的支持。因此在安装groovy时首先要安装JDK。
JDK安装步骤:
[list]
[*]下载最新的JDK版本。(下载网址:http://java.sun.com)
[*]运行安装文件。(更改安装路径到:D:\jdk\版本号)
[*]设置JAVA_HOME环境变量。
[*]在系统path中增加:%JAVA_HOME%\bin
[/list]
注:对于1.1-rc-1以上版本需要JDK1.5版本。
2、安装groovy环境
[list]
[*]下载最新的zip形式的groovy版本。(下载地址:http://groovy.codehaus.org/Download)
下载后的文件为:groovy-binary-1.5.1.zip
[*]解压到groovy的安装目录(D:\groovy\1.5.1\)
[*]设置GROOVY_HOME环境变量。
[*]在系统path中增加:%GROOVY_HOME%\bin
[/list]
3、安装可选的jar包
将groovy用到的jar包放在%GROOVY_HOME%\lib也可在groovy-starter.conf文件中设定加载目录
4、HelloWorld,Groovy!
打开groovyConsole(双击%GROOVY_HOME%\bin下的groovyConsole.bat)
输入:
println " HelloWorld,Groovy!"
运行(ctrl+r)输出:
HelloWorld,Groovy!
输入:
1+1*9
运行输出:
10
5、Groovy的变量
输入:
x = 1;
println x;
x = -3.1415926
println x;
x = false;
println x;
x = new java.util.Date();
println x;
x = "hi"
println x;
运行输出:
1
-3.1415926
false
Mon Jan 07 10:50:42 CST 2008
hi
6、Groovy的list和map
List
输入:
myList = [20,-3.5,false,"hi"];
println myList[3];
println myList.size();
println myList[4];
运行输出:
hi
4
Null
空List:emptyList = []
Map
输入:
scores = [ "Brett":100, "Pete":"Did not finish", "Andrew":86.87934 ]
println scores["Pete"];
println scores.Pete
scores["Pete"] = 8;
println scores.Pete;
scores.Pete = 3;
println scores.Pete;
运行输出:
Did not finish
Did not finish
8
3
空Map:emptyMap = [:]
7、条件表达式
输入:
scores = [ "Brett":100, "Pete":"Did not finish", "Andrew":86.87934 ]
if (scores.Brett != null) {
println "scores.Brett="+scores.Brett
} else {
println "scores.Brett=null"
}
运行输出:
scores.Brett=100
8、Boolean表达式
输入:
titanicBoxOffice = 1234600000
titanicDirector = "James Cameron"
trueLiesBoxOffice = 219000000
trueLiesDirector = "James Cameron"
returnOfTheKingBoxOffice = 752200000
returnOfTheKingDirector = "Peter Jackson"
theTwoTowersBoxOffice = 581200000
theTwoTowersDirector = "PeterJackson"
println titanicBoxOffice > returnOfTheKingBoxOffice // evaluates to true
println titanicBoxOffice >= returnOfTheKingBoxOffice // evaluates to true
println titanicBoxOffice >= titanicBoxOffice // evaulates to true
println titanicBoxOffice > titanicBoxOffice // evaulates to false
println titanicBoxOffice + trueLiesBoxOffice < returnOfTheKingBoxOffice + theTwoTowersBoxOffice // evaluates to false
println titanicDirector > returnOfTheKingDirector // evaluates to false, because "J" is before "P"
println titanicDirector < returnOfTheKingDirector // evaluates to true
println titanicDirector >= "James Cameron" // evaluates to true
println titanicDirector == "James Cameron"
运行输出:
true
true
true
false
false
false
true
true
true
[url=http://www.51.la/?1613417][img]http://img.users.51.la/1613417.asp[/img][/url]
1、安装JDK环境
Groovy需要JDK1.4以上版本的支持。因此在安装groovy时首先要安装JDK。
JDK安装步骤:
[list]
[*]下载最新的JDK版本。(下载网址:http://java.sun.com)
[*]运行安装文件。(更改安装路径到:D:\jdk\版本号)
[*]设置JAVA_HOME环境变量。
[*]在系统path中增加:%JAVA_HOME%\bin
[/list]
注:对于1.1-rc-1以上版本需要JDK1.5版本。
2、安装groovy环境
[list]
[*]下载最新的zip形式的groovy版本。(下载地址:http://groovy.codehaus.org/Download)
下载后的文件为:groovy-binary-1.5.1.zip
[*]解压到groovy的安装目录(D:\groovy\1.5.1\)
[*]设置GROOVY_HOME环境变量。
[*]在系统path中增加:%GROOVY_HOME%\bin
[/list]
3、安装可选的jar包
将groovy用到的jar包放在%GROOVY_HOME%\lib也可在groovy-starter.conf文件中设定加载目录
4、HelloWorld,Groovy!
打开groovyConsole(双击%GROOVY_HOME%\bin下的groovyConsole.bat)
输入:
println " HelloWorld,Groovy!"
运行(ctrl+r)输出:
HelloWorld,Groovy!
输入:
1+1*9
运行输出:
10
5、Groovy的变量
输入:
x = 1;
println x;
x = -3.1415926
println x;
x = false;
println x;
x = new java.util.Date();
println x;
x = "hi"
println x;
运行输出:
1
-3.1415926
false
Mon Jan 07 10:50:42 CST 2008
hi
6、Groovy的list和map
List
输入:
myList = [20,-3.5,false,"hi"];
println myList[3];
println myList.size();
println myList[4];
运行输出:
hi
4
Null
空List:emptyList = []
Map
输入:
scores = [ "Brett":100, "Pete":"Did not finish", "Andrew":86.87934 ]
println scores["Pete"];
println scores.Pete
scores["Pete"] = 8;
println scores.Pete;
scores.Pete = 3;
println scores.Pete;
运行输出:
Did not finish
Did not finish
8
3
空Map:emptyMap = [:]
7、条件表达式
输入:
scores = [ "Brett":100, "Pete":"Did not finish", "Andrew":86.87934 ]
if (scores.Brett != null) {
println "scores.Brett="+scores.Brett
} else {
println "scores.Brett=null"
}
运行输出:
scores.Brett=100
8、Boolean表达式
输入:
titanicBoxOffice = 1234600000
titanicDirector = "James Cameron"
trueLiesBoxOffice = 219000000
trueLiesDirector = "James Cameron"
returnOfTheKingBoxOffice = 752200000
returnOfTheKingDirector = "Peter Jackson"
theTwoTowersBoxOffice = 581200000
theTwoTowersDirector = "PeterJackson"
println titanicBoxOffice > returnOfTheKingBoxOffice // evaluates to true
println titanicBoxOffice >= returnOfTheKingBoxOffice // evaluates to true
println titanicBoxOffice >= titanicBoxOffice // evaulates to true
println titanicBoxOffice > titanicBoxOffice // evaulates to false
println titanicBoxOffice + trueLiesBoxOffice < returnOfTheKingBoxOffice + theTwoTowersBoxOffice // evaluates to false
println titanicDirector > returnOfTheKingDirector // evaluates to false, because "J" is before "P"
println titanicDirector < returnOfTheKingDirector // evaluates to true
println titanicDirector >= "James Cameron" // evaluates to true
println titanicDirector == "James Cameron"
运行输出:
true
true
true
false
false
false
true
true
true
[url=http://www.51.la/?1613417][img]http://img.users.51.la/1613417.asp[/img][/url]