Groovy基本句法
Groovy基本句法
Gradle作为一个构建工具自然不会自己去创造一门语言来支撑自己,那么它用的是哪门子语言呢?什么语言能写成这样:
task hello {
doLast {
println 'Hello world!'
}
}
如此风骚的语法自然要归Groovy莫属了。
什么是Groovy
官方介绍如下:
Apache Groovy is a powerful, optionally typed and dynamic language, with static-typing and static compilation capabilities, for the Java platform aimed at improving developer productivity thanks to a concise, familiar and easy to learn syntax. It integrates smoothly with any Java program, and immediately delivers to your application powerful features, including scripting capabilities, Domain-Specific Language authoring, runtime and compile-time meta-programming and functional programming.
大概意思是Groovy是一门运行在java平台上的强大的、可选类型的、动态语言。使用Groovy可以使你的应用具备脚本,DSL定义,运行时和编译时元编程,函数式编程等功能。
接下来将分几个小节简单介绍Groovy的语法规范。
Groovy语法
注释
Groovy使用的注释有一下几种:
1.单行注释
// a standalone single line comment
println "hello" // a comment till the end of the line
2.多行注释
/* a standalone multiline comment
spanning two lines */
println "hello" /* a multiline comment starting
at the end of a statement */
println 1 /* one */ + 2 /* two */
3.文档注释
/**
* A Class description
*/
class Person {
/** the name of the person */
String name
/**
* Creates a greeting method for a certain person.
*
* @param otherPerson the person to greet
* @return a greeting message
*/
String greet(String otherPerson) {
"Hello ${otherPerson}"
}
}
4.组织行
#!/usr/bin/env groovy
println "Hello from the shebang line"
这类脚本注释主要用于表明脚本的路径。
字符串
单引号字符串
单引号字符串对应java中的String,不支持插入。
'a single quoted string'
字符串连接
assert 'ab' == 'a' + 'b'
三引号字符串
'''a triple single quoted string'''
三引号字符串同样对应java中的String,不支持动态插入。三引号字符串支持多行:
def aMultilineString = '''line one
line two
line three'''
转义
Groovy中使用\来进行转义
'an escaped single quote: \' needs a backslash'
双引号字符串
"a double quoted string"
如果双引号字符串中没有插入表达式的话对应的是java中的String对象,如果有则对应Groovy中的GString对象。
Groovy中使用${}来表示插入表达式,$来表示引用表达:
def name = 'Guillaume' // a plain string
def greeting = "Hello ${name}"
assert greeting.toString() == 'Hello Guillaume'
def person = [name: 'Guillaume', age: 36]
assert "$person.name is $person.age years old" == 'Guillaume is 36 years old'
shouldFail(MissingPropertyException) {
println "$number.toString()"
}
插入闭包表达式
def sParameterLessClosure = "1 + 2 == ${-> 3}"
assert sParameterLessClosure == '1 + 2 == 3'
def sOneParamClosure = "1 + 2 == ${ w -> w << 3}"
assert sOneParamClosure == '1 + 2 == 3'
def number = 1
def eagerGString = "value == ${number}"
def lazyGString = "value == ${ -> number }"
assert eagerGString == "value == 1"
assert lazyGString == "value == 1"
number = 2
assert eagerGString == "value == 1"
assert lazyGString == "value == 2"
关于闭包,暂时先看看就行,等后面具体学习完闭包以后再回来看这几个表达式就简单了。
三双引号字符串
def name = 'Groovy'
def template = """
Dear Mr ${name},
You're the winner of the lottery!
Yours sincerly,
Dave
"""
assert template.toString().contains('Groovy')
斜杠字符串
Groovy也可以使用/来定义字符串,主要用于正则表达式
def fooPattern = /.*foo.*/
assert fooPattern == '.*foo.*'
def escapeSlash = /The character \/ is a forward slash/
assert escapeSlash == 'The character / is a forward slash'
def multilineSlashy = /one
two
three/
assert multilineSlashy.contains('\n')
def color = 'blue'
def interpolatedSlashy = /a ${color} car/
assert interpolatedSlashy == 'a blue car'
\(/和/\)字符串
def name = "Guillaume"
def date = "April, 1st"
def dollarSlashy = $/
Hello $name,
today we're ${date}.
$ dollar sign
$$ escaped dollar sign
\ backslash
/ forward slash
$/ escaped forward slash
$/$ escaped dollar slashy string delimiter
/$
assert [
'Guillaume',
'April, 1st',
'$ dollar sign',
'$ escaped dollar sign',
'\\ backslash',
'/ forward slash',
'$/ escaped forward slash',
'/$ escaped dollar slashy string delimiter'
].each { dollarSlashy.contains(it) }
字符
单引号字符串如果只有一个字符会被转化成char类型。
列表
Groovy中列表使用[]表示,其中可以包含任意类型的元素:
def heterogeneous = [1, "a", true]
使用下标进行取值和赋值
def letters = ['a', 'b', 'c', 'd']
assert letters[0] == 'a'
assert letters[1] == 'b'
assert letters[-1] == 'd'
assert letters[-2] == 'c'
letters[2] = 'C'
assert letters[2] == 'C'
letters << 'e'
assert letters[ 4] == 'e'
assert letters[-1] == 'e'
assert letters[1, 3] == ['b', 'd']
assert letters[2..4] == ['C', 'd', 'e']
数组
Groovy中复用List来充当数组,但如果要明确定义真正的数组需要使用类似java的定义方法
String[] arrStr = ['Ananas', 'Banana', 'Kiwi']
assert arrStr instanceof String[]
assert !(arrStr instanceof List)
def numArr = [1, 2, 3] as int[]
assert numArr instanceof int[]
assert numArr.size() == 3
键值数组
Groovy中键值数组使用如下
def colors = [red: '#FF0000', green: '#00FF00', blue: '#0000FF']
assert colors['red'] == '#FF0000'
assert colors.green == '#00FF00'
colors['pink'] = '#FF00FF'
colors.yellow = '#FFFF00'
assert colors.pink == '#FF00FF'
assert colors['yellow'] == '#FFFF00'
assert colors instanceof java.util.LinkedHashMap
以上简单列举了Groovy的基本语法,想要深入学习的可以参考以下网址:
运行syntax.groovy查看实例及运行结果
安装Groovy参考如下:
http://www.groovy-lang.org/download.html
Groovy基本句法的更多相关文章
- Groovy中的脚本与类
包名 当你在groovy中定义类的时候需要指定包名,这和java中类似不多介绍. 导入 groovy中的导入也跟java类似,有一下五种: 默认导入 groovy默认导入了一下几个包和类: impor ...
- groovy语法
1.注释1.1. 单行注释1.2. 多行注释1.3. GroovyDoc注释1.4. Shebang线2.关键词3.标识符3.1. 普通标识符3.2. 带引号的标识符4.字符串4.1. 单引号字符串4 ...
- Groovy中的闭包
Closures(闭包) 本节主要讲groovy中的一个核心语法:closurs,也叫闭包.闭包在groovy中是一个处于代码上下文中的开放的,匿名代码块.它可以访问到其外部的变量或方法. 1. 句法 ...
- Groovy sort() list
https://www.w3cschool.cn/groovy/groovy_sort.html #Groovy sort()方法返回原始列表的排序副本. #句法List sort() #参数没有 # ...
- Java——搭建自己的RESTful API服务器(SpringBoot、Groovy)
这又是一篇JavaWeb相关的博客,内容涉及: SpringBoot:微框架,提供快速构建服务的功能 SpringMVC:Struts的替代者 MyBatis:数据库操作库 Groovy:能与Java ...
- 用Groovy构建java脚本
我是做工作流项目的,工作流中各个模板引擎都需要要执行一个动态业务,这些动态业务有多种实现方式,最常用的就是用户自己写一段脚本文件,然后工作流引擎执行到这里的时候,运行这个脚本文件. 这个运行脚本文件的 ...
- Groovy学习--基本语法了解
x项目用到gradle,学习gradle之前准备先过一遍Groovy的语法.这里参考:Groovy入门. 该博客没有系统的讲解Groovy的语法和原理,仅仅只是罗列了使用Groovy的常规方法.我照着 ...
- How to use groovy script on jenkins
1. Install groovy plugin 2. Add a step of groovy. (normal & systerm) 3. Execute groovy script im ...
- Java8-Function使用及Groovy闭包的代码示例
导航 定位 概述 代码示例 Java-Function Groovy闭包 定位 本文适用于想要了解Java8 Function接口编程及闭包表达式的筒鞋. 概述 在实际开发中,常常遇到使用模板模式的场 ...
随机推荐
- HttpOperater
using System; using System.IO; using System.Linq; using System.Net; using System.Text; using System. ...
- openWRT学习之LUCI之中的一个helloworld演示样例
备注1:本文 讲述的是原生的openWRT环境下的LUCI 备注2:本文參考了诸多资料.感谢网友分享.參考资料: http://www.cnblogs.com/zmkeil/archive/2013/ ...
- 二级指针 (C语言)
二级指针又叫双指针.C语言中不存在引用,所以当你试图改变一个指针的值的时候必须使用二级指针.C++中可以使用引用类型来实现. 下面讲解C中的二级指针的使用方法. 例如我们使用指针来交换两个整型变量的值 ...
- 分享一个mysql 复杂查询的例子
发布:脚本学堂/MySQL 编辑:thebaby 2013-08-23 09:37:37 [大 中 小] 有关mysql复杂查询的一个例子,正在学习mysql的朋友,可以作为一个参考. 在my ...
- 最新的Zynq资料整理
1.Zynq修炼秘籍 基于ZYNQ的FPGA基础入门 基于ZYNQ的Soc入门基础 基于ZYNQ的裸机应用开发 基于ZYNQ硬件的LINUX开发 基于ZYNQ的HLS图像算法设计 基于ZYNQ的图像处 ...
- 彻底清除Linux centos minerd木马 实战 跟redis的设置有关
top -c把cpu占用最多的进程找出来: Tasks: total, running, sleeping, stopped, zombie Cpu(s): 72.2%us, 5.9%sy, 0.0% ...
- android.view.animation(2) - 插值器Interpolator
public interface Interpolator implements TimeInterpolator android.view.animation.Interpolator Known ...
- C语言 · 复数四则运算
算法提高 6-17复数四则运算 时间限制:1.0s 内存限制:512.0MB 设计复数库,实现基本的复数加减乘除运算. 输入时只需分别键入实部和虚部,以空格分割,两个复数之间用运算符 ...
- mf210v 端口的映射
ttyUSB0 : 诊断端口 ttyUSB1 : AT指令端口 ttyUSB2 : VoUSB端口(语音) ttyUSB3 : Modem端口
- 启动BusyBox内建的FTP Server
启动BusyBox内建的FTP Server 要启动BusyBox内建的FTP Server,我们需要先孰悉tcpsvd与ftpd这两个命令. tcpsvd可以建立TCP socket,并将它bi ...