Apache Groovy是一种强大的、可选类型的动态语言,具备静态类型和静态编译功能,因为具备简洁、熟悉和易于学习的语法,Java平台旨在提升开发人员的工做效率。它能够与任何Java程序顺畅地集成,并当即向应用程序交付强大的特性,包括脚本功能、特定领域的语言创做、运行时和编译时元编程以及函数式编程(官方文档对groovy描述)。html
brew install groovy
复制代码
You should set GROOVY_HOME:
export GROOVY_HOME=/usr/local/opt/groovy/libexec
复制代码
groovy -v
## 打印输出
Groovy Version: 3.0.4 JVM: 14.0.1 Vendor: Oracle Corporation OS: Mac OS X
复制代码
def a = 1
def s = "test"
def int b = 1 //定义变量也能够知道类型
复制代码
def test(arg1,arg2){ //定义两个参数 arg1 arg2
//逻辑代码
....
// 函数最后一行为返回值 函数返回值为 int 类型
10
}
String test2(arg1,arg2){ //指定返回值类型 String 则没必要写关键字 def
//逻辑代码
....
// 函数最后一行为返回值 函数返回值为 String 类型
//函数指定返回值 则返回值必须一致
// return 可写可不写
return "aaa"
}
复制代码
//打印输出 hello
println("hello")
//能够写成
println "hello"
复制代码
println("hello groovy")
复制代码
def a = 1
def b = "hello groovy"
def c = false
def d = 10000000000000
println a.getClass().getCanonicalName()
println b.getClass().getCanonicalName()
println c.getClass().getCanonicalName()
println d.getClass().getCanonicalName()
复制代码
//随意添加各类类型对象 变量打印
def aList = [5,'string',true]
//元素变量
aList.each {
//it是是与当前元素对应的隐式参数
println "Item: $it"
}
//添加元素
//查找元素 find 方法
//
println(aList.find{ it > 1 })
println(aList.findAll{ it > 1 })
//删除元素
//执行结果
Item: 5
Item: string
Item: true
复制代码
//其中的 key1 和 key2 默认被处理成字符串"key1"和"key2"
def aNewMap = [key1:"hello",key2:false]
//map 取值
println aNewMap.key1
println aNewMap['key2']
//为 map 添加新元素
aNewMap.anotherkey = "i am map"
aNewMap.each{
println "Item: $it"
}
//执行结果
hello
false
Item: key1=hello
Item: key2=false
Item: anotherkey=i am map
复制代码
//标识 list 至关于数学闭包 [1,5]
def mRange = 1..5
mRange.each {
println "Item: $it"
}
//标识 list 至关于数学闭包 [1,5)
def mRange1 = 1..<5
mRange1.each {
println "other Item: $it"
}
//获取开头结尾元素
println mRange.from
println mRange.to
复制代码
//格式一 有参数
def xxx = {
params ->
//返回值
code逻辑
}
//格式二 无参数 不须要 ->
def xxx = {
code 逻辑
}
复制代码
//闭包是一段代码,因此须要用花括号括起来
def testClosure = {
//箭头前面是参数定义,箭头后面是代码
String param1, int param2 ->
//逻辑代码,闭包最后一句是返回值
println "hello groovy,$param1,$param2"
//也可使用 return,和 groovy 中普通函数同样
}
//闭包调用
testClosure.call("参数1",20)
testClosure("参数2",40)
//输出结果
hello groovy,参数1,20
hello groovy,参数2,40
复制代码
似,it 表明闭包的参数,以下示例。java
def greeting = {
//隐含参数
"Hello, $it!"
}
println greeting('groovy') == 'Hello, groovy!'
//等同于:
def greeting1 = {
//也可写出隐含参数
it -> "Hello, $it!"
}
println greeting1('groovy') == 'Hello, groovy!'
//输出结果 不用说确定都为 true
复制代码
task hello{
doLast ({
//逻辑代码
println'aaaaaa'
})
}
//省略括号变成经常使用写法
task hello{
doLast {
//逻辑代码
println'aaaaaa'
}
}
复制代码
def testFile = new File("TestFile")
//读文件每一行 eachLine
testFile.eachLine{
String oneLine ->
//打印每一行内容
println oneLine
}
//获取文件 byte 数组
def bytes = testFile.getBytes()
//获取文件输入流
def is = testFile.newInputStream()
//和Java 同样不用须要关闭
is.close
//闭包 Groovy 会自动替你 close
targetFile.withInputStream{ ips ->
//逻辑代码
}
复制代码
def copyFile = new File("CopyFile")
copyFile.withOutputStream{
os->
testFile.withInputStream{
ips ->
// << 是 groovy OutputStream 的操做符,它能够完成 InputStream 到 OutputStream 输出
os << ips
}
}
copyFile.eachLine{
String oneLine ->
println "copyFile oneLine:$oneLine"
}
复制代码
//建立 XmlSlurper 类
def xmlspr = new XmlSlurper()
//获取清单文件 file
def file = new File("AndroidManifest.xml")
//获取解析对象
//获取清单文件根元素,也就是 manifest 标签
def manifest = xmlspr.parse(file)
// 声明命名空间
//manifest.declareNamespace('android':'http://schemas.android.com/apk/res/android')
//获取包名
println manifest.'@package'
//获取 activity intent-filter
def activity = manifest.application.activity
//获取 intent-filter 设置的过滤条件 也能够此判断是否为应用程序入口 activity
activity.find{
it.'intent-filter'.find { filter ->
filter.action.find{
println it.'@android:name'.text()
}
filter.category.find{
println it.'@android:name'.text()
}
}
}
复制代码
groovy File api 文档地址android
groovy OutputStreamgithub
package groovyclass
class GroovyClass{
String p1;
int p2;
GroovyClass(p1,p2){
this.p1 = p1
this.p2 = p2
}
//和 Java 相似 若是不声明 public/private
//等访问权限的话,Groovy 中类及其变量默认都是 public
def printParams(){
println "参数:$p1,$p2"
}
}
复制代码
import groovyclass.GroovyClass
def g = new GroovyClass("hello",100)
g.printParams()
复制代码
println("hello groovy")
复制代码
groovyc -d classes test.groovy
复制代码