我的漫漫程序之旅

专注于JavaWeb开发
随笔 - 39, 文章 - 310, 评论 - 411, 引用 - 0
数据加载中……

Groovy之旅系列二(初识Groovy基本语法)

    昨天我们写了一个HelloWorld,其实很简单的.呵呵.
现在我们打开Groovy控制台输入:
123+45*67
按Ctrl+R,结果就会输出来了.

Result: 3138

  现在我们来看看给变更赋值

= 1
println x

= new java.util.Date()
println x

= -3.1499392
println x

= false
println x

= "Hi"
println x

Groovy在你需要用时才给变量赋予类型和值.

这在Java里是不可想象的.

List和Maps:

我们来看看如何来声明一个集合:
myList = [1776-133990928734928763]

和Java一样,集合的索引是从0开始的.你可以这样访问:
println myList[0]

将会输出:
1776
你能得到集合的长度
println myList.size()
将会输出:
6

来看看Map怎样声明:
scores = [ "Brett":100"Pete":"Did not finish""Andrew":86.87934 ]

注意每个键的值类型都是不同的.
现在我们访问一下键为"Pete"的值,有两种方式:
println scores["Pete"]
println scores.Pete

会输出:
Did not finish
Did not finish

我们也能给scores["Pete"]赋予新值

scores["Pete"= 3
再次访问scores["Pete"]
println scores["Pete"]

将会输出3

你也可以创建一个空集合和空Map:

emptyMap = [:]
emptyList 
= []

为了确保集合或Map是空的,你可以输出一个它们的大小:
println emptyMap.size()
println emptyList.size()
输出是0

现在我们来看看条件执行吧:
amPM = Calendar.getInstance().get(Calendar.AM_PM)
if (amPM == Calendar.AM)
{
    println(
"Good morning")
}
 else {
    println(
"Good evening")
}


这是一个简单的判断是上午还是下午的小程序,对于第一行你可以参考Groovy-doc.

Bool表达式:

myBooleanVariable = true
当然还有一些复杂的bool表达式:
* ==
* !=
* >
* >=
* <
* <=
来看看一些例子吧:
titanicBoxOffice = 1234600000
titanicDirector 
= "James Cameron"

trueLiesBoxOffice 
= 219000000
trueLiesDirector 
= "James Cameron"

returnOfTheKingBoxOffice 
= 752200000
returnOfTheKingDirector 
= "Peter Jackson"

theTwoTowersBoxOffice 
= 581200000
theTwoTowersDirector 
= "PeterJackson"

titanicBoxOffice 
> returnOfTheKingBoxOffice  // evaluates to true
titanicBoxOffice >= returnOfTheKingBoxOffice // evaluates to true
titanicBoxOffice >= titanicBoxOffice         // evaulates to true
titanicBoxOffice > titanicBoxOffice          // evaulates to false
titanicBoxOffice + trueLiesBoxOffice < returnOfTheKingBoxOffice + theTwoTowersBoxOffice  // evaluates to false

titanicDirector 
> returnOfTheKingDirector    // evaluates to false, because "J" is before "P"
titanicDirector < returnOfTheKingDirector    // evaluates to true
titanicDirector >= "James Cameron"           // evaluates to true
titanicDirector == "James Cameron"           // evaluates to true

bool表达式对于if来说是非常有用的:
if (titanicBoxOffice + trueLiesBoxOffice > returnOfTheKingBoxOffice + theTwoTowersBoxOffice)
{
    println(titanicDirector 
+ " is a better director than " + returnOfTheKingDirector)
}
再看关于天气的例子:
suvMap = ["Acura MDX":"\$36,700""Ford Explorer":"\$26,845"]
if (suvMap["Hummer H3"!= null)
{
     println(
"A Hummer H3 will set you back "+suvMap["Hummer H3"]);
}

ok,今天到此为止吧.

posted on 2008-04-29 09:00 々上善若水々 阅读(2501) 评论(4)  编辑  收藏

评论

# re: Groovy之旅系列二(初识Groovy基本语法)  回复  更多评论   

继续努力.希望出一个系列.
2008-04-29 10:51 | java begginger

# re: Groovy之旅系列二(初识Groovy基本语法)[未登录]  回复  更多评论   

初学者的例子
2008-04-29 10:52 | BeanSoft

# re: Groovy之旅系列二(初识Groovy基本语法)[未登录]  回复  更多评论   

准备学习Groovy,看来也没那么复杂.
2008-04-29 10:52 | shy

# re: Groovy之旅系列二(初识Groovy基本语法)[未登录]  回复  更多评论   

不错,欢迎到国内第一个基于Grails开发的并且讨论Java&Grails技术网站
www.javaread.com分享你的技术文章。
2008-08-13 17:54 | javaread.com

只有注册用户登录后才能发表评论。


网站导航: