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 - 1 33 99 0 928734928763 ]

和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,今天到此为止吧.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值