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 commentprintln "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 groovyprintln "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 oneline twoline three'''转义
Groovy中使用\
来进行转义
'an escaped single quote: \' needs a backslash'双引号字符串
"a double quoted string"
如果双引号字符串中没有插入表达式的话对应的是java中的String对象,如果有则对应Groovy中的GString对象。
${}来表示插入表达式,$
来表示引用表达:
def name = 'Guillaume' // a plain stringdef 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基本句法全部内容,希望文章能够帮你解决Groovy基本句法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)