(-)变量
你可以给变量赋值. 像下面:
x = 1
println x
x = new java.util.Date()
println x
x = -3.1499392
println x
x = false
println x
x = "Hi"
println x
(二)Lists and Maps
Groovy已经内置支持两种重要的数据类型, lists 和maps((Lists就像java中的数组一样来操作))Lists用来存储有序集合数据,例如:
myList = [1776, -1, 33, 99, 0, 928734928763]
你可以使用[]来存取给定的项(索引开始位置为0):
println myList[0]
输出结果:
1776
你也可以使用size方法来等到 list 长度:
println myList.size()
输出结果:
6
但是通常你并不需要List的长度,因为不像Java,循环遍历一个List中的所有元素的首选方法是each(),详细信息在本学习笔记的"Code as Data"章节。
另一个本地数据结构叫 map,map用来存储”关联数组“或”词典“,即无序非均匀的集合,命名数据。例如,我们存储名字的IQ分数:
scores = [ "Brett":100, "Pete":"Did not finish", "Andrew":86.87934 ]
注意,每个存储在map中的值都是不同的类型, Brett'是 integer,Pete是string, Andrew是floating 。
我们可以使用两种方法来访问map中的值:
println scores["Pete"]
println scores.Pete
输出结果:
Did not finish
Did not finish
为map添加数据,语法与给list添加值类似,例如:
scores["Pete"] = 3
随后取得其值:
println scores["Pete"]
输出结果:
3.
另外,你可以像下面这样创建空map和list:
emptyMap = [:](Map)
emptyList = [](List)
为了确定它们为空,可以像下列这样:
println emptyMap.size()
println emptyList.size()
输出结果为0。
(三)Conditional Execution
任何一种语言的重要特性是在不同的条件下执行不懂的代码,简单的方法是使用"if":
amPM = Calendar.getInstance().get(Calendar.AM_PM)
if (amPM == Calendar.AM)
{
println("Good morning")
} else {
println("Good evening")
}
上面的结果是:首先,他会判断()中表达式,随后依据结果是否为"true"或"false"来执行第一个或第2个代码块。注意"else"代码块不是必须的:
amPM = Calendar.getInstance().get(Calendar.AM_PM)
if (amPM == Calendar.AM)
{
println("Have another cup of coffee.")
}
(四)Boolean Expressions
在很多编程语言中这个是很特别的数据类型,它用来表示真实值,”true“或"false".Boolean就像其他数据类型一样可以储存在一个变量中:
myBooleanVariable = true
更多Boolean表达式操作符:
* ==
* !=
* >
* >=
* <
* <=
他们相当直观:
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
分享到:
相关推荐
Groovy 学习笔记 Groovy 学习笔记
Groovy
Java中使用Groovy的三种方式,详细见我的博客。
Groovy 调用 Java 类groovy 调用 Java class 十分方便,只需要在类前导入该 Java 类,在 Groovy 代码中就可以无缝使用该
Groovy学习笔记,内容如下: 1.概念 2.基本语法 3.字符串 4.集合 5.类和闭包 6.控制语句 7.操作符重载 8.I/O操作 9.高级应用 10.正则表达式
Grails is a Java- and Groovy-based web framework that is built for speed. First-time developers are amazed at how quickly you can get a page-centric MVC web site up and running thanks to the ...
对Groovy开发环境设置,基本语法做了简单明了的介绍
Java调用Groovy,实时动态加载数据库groovy脚本,java读取mongoDB的groovy脚本,加载实时运行,热部署
Making Java Groovy is a practical handbook for developers who want to blend Groovy into their day to day work with Java It starts by introducing the key differences between Java and Groovy and how you...
概述主要介绍Java、spring与groovy结合使用,高清英文版本
Making Java Groovy
java 动态脚本语言 精通 Groovy
java运行groovy 环境,搭建方便自己练习groovy使用 与后续定时调度动态代码做准备
第1章 数字和字符串 第2章 List的简单介绍 第3章 String的特殊用法 第4章 闭包 第5章 集合+闭包 第6章 Map和Range 第7章 函数
Groovy环境搭建教程中的例子工程,纯Java、纯Groovy以及Java+Groovy混合 教程参考:http://blog.csdn.net/rcom10002/archive/2011/06/26/6568557.aspx
SpringBoot-Gradle-Maven-Java-Groovy:SpringBoot通过不同构建工具和语言实现。
图文并茂,适合入门新手,涵盖了基本的语法讲解及一些DEMO测试,值得一看
Making Java Groovy Kenneth A. Kousen 1.Easier Java 2.Closures, builders, and metaprogramming 3.Gradle for builds, Spock for testing 4.Groovy frameworks like Grails and Griffon 源码
Making Java Groovy--Kenneth.A.Kousen. Java Groovy 很不错的学习资料