Groovy 是在java 基础上改造的脚本风格的语言,运行与JVM平台,可与java 无缝集成。
环境安装:
1,JDK 1.8
2,在 Groovy 官网下载压缩包 http://www.groovy-lang.org/download.html,解压到磁盘目录,将根路径加入到环境变量,和jdk的安装方法类似
3,在cmd 中运行命令 : groovy -version
出现类似这种信息:Groovy Version: 2.5.1 JVM: 1.8.0_45 Vendor: Oracle Corporation OS: Windows 7
说明安装成功。
基本语法:
1,代码注释与java 一样
2,语句结尾可不用分号
3,单引字符串号只是一个简单的字符串
4,双引号字符串,可以插入变量
1 def a = 'tom' 2 def str = 'hello $a' 3 println(str) 4 def str1 = "hello $a" 5 println(str1)
输出:
1 hello $a 2 hello tom
5,三引号字符串会保留格式
1 def str2 = """ hi! 2 tom """ 3 4 println(str2)
输出:
1 hi! 2 tom
6,变量定义,Groovy 支持动态类型,可以使用def 关键字定义变量,而不用给出具体类型,def 可以省略。
7,函数定义,无返回的函数必须指定返回类型为def ,函数最后一句作为返回值,函数调用可以省略括号()
1 //无参函数 2 def fun1(){ 3 2 4 } 5 6 println(fun1()) 7 println fun1() 8 //有参函数 , 无需指定参数类型 9 def fun2( def1 , def2 ){ 10 11 }
输出:
1 2 2 2
8,断言 Assert
1 str2 = """ hi! 2 tom """ 3 4 assert str2 == null
输出:
1 Caught: Assertion failed: 2 3 assert str2 == null 4 | | 5 | false 6 ' hi!\n tom '
断言失败程序将会终止
9,for 循环
第一种,和java 类似
1 for (i = 0; i < 5 ; i++) { 2 println(i) 3 }
输出:
1 0 2 1 3 2 4 3 5 4
第二种:
1 for (i in 0..5){ 2 println("hello world"+i) 3 }
输出:
1 hello world0 2 hello world1 3 hello world2 4 hello world3 5 hello world4 6 hello world5
注意此处会包含上界
10,time 循环
1 5.times{ 2 println("time"+it) 3 }
输出:
1 time0 2 time1 3 time2 4 time3 5 time4
不包含上界,it 相当于java 中的this
11,三木运算
1 a = 1==1?:"no" 2 println(a) 3 4 a = 1==2?:"no" 5 println(a)
输出:
1 true 2 no
12,异常捕获
1 try{ 2 a = 1/0 3 }catch (Exception e){ 4 println(e) 5 } 6 7 try{ 8 a = 1/0 9 }catch (e){ 10 println(e) 11 } 12 println("运行")
输出:
1 java.lang.ArithmeticException: Division by zero 2 java.lang.ArithmeticException: Division by zero 3 运行
13,switch 结构
1 a = 5 2 switch(a){ 3 case 1: 4 println(1) 5 break 6 case 1..5: 7 println("1 ~ 5") 8 break 9 case 5..<10: 10 println("a > 5") 11 break 12 default: 13 println("大于 9 或 不是数字") 14 }
输出:
1 1 ~ 5
14,判断非空的便捷方式
1 class Person { 2 String name 3 Person child 4 } 5 p1 = new Person() 6 p1.child = new Person() 7 p1.child.name = 'tom' 8 p2 = new Person() 9 p2.child = null 10 11 println p1?.child?.name 12 println p2?.child?.name
输出:
1 tom 2 null
15,类型转换 asType
1 s1 = 1 2 //String 转成 int 3 def s2 = s1 as int 4 5 //String 转成 int 6 def s3 = s1.asType(Integer) 7 println(s2.class) 8 println(s3.class)
输出:
1 class java.lang.Integer 2 class java.lang.Integer
Groovy 没有typeof 关键字
16,Groovy 数据类型:
groovy 中的数据类型分两种,
一种是java 中的基本数据类型,
一种是groovy 中的容器类,还有闭包
java基本数据类型:
1 def boolean s1 = true 2 3 def int s2 = 100 4 5 def String s3 = "hello world" 6 7 if (s1) { 8 println("hello world") 9 }
Groovy 容器类型:
List:列表,内部是java 的 ArrayList
1 //列表由[]定义 2 def list = [1,2,3,"tom"] 3 println(list) 4 println(list.size()) 5 println(list[1]) 6 7 //添加加元素 8 list << true 9 10 println(list) 11 12 //下标超过返回Null 13 println(list[5])
输出:
1 [1, 2, 3, tom] 2 4 3 2 4 [1, 2, 3, tom, true] 5 null
Map:K-V序列,内部是LinkHashMap 实现,有序的
1 //定义map 2 def map = [name:"tom",age:12,score:100,isMale:false] 3 println(map) 4 //获取长度 5 println(map.size()) 6 //获取key set 7 println(map.keySet()) 8 //获取值 9 println(map.name) 10 println(map.get("name")) 11 12 // 添加,修改 元素 13 map.put("name","bob") 14 println(map.name) 15 Iterator it = map.iterator() 16 while (it.hasNext()) { 17 println "遍历map: " + it.next() 18 } 19 20 map.containsKey("name") 21 22 map.containsValue("tom") 23 24 //删除元素 25 map.remove("name") 26 println(map)
输出:
1 [name:tom, age:12, score:100, isMale:false] 2 4 3 [name, age, score, isMale] 4 tom 5 tom 6 bob 7 遍历map: name=bob 8 遍历map: age=12 9 遍历map: score=100 10 遍历map: isMale=false 11 [age:12, score:100, isMale:false]
Range:范围
1 def range = 1..5 2 println(range) 3 4 //两种遍历方法 5 for(i in range){ 6 println(">>"+i) 7 } 8 9 iter = range.iterator() 10 while(iter.hasNext()){ 11 println(iter.next()) 12 } 13 14 //长度 15 printf("长度 : %s \n",range.size()) 16 17 //获取指定下标元素 18 printf("下标1的元素是: %s \n",range.get(1)) 19 20 //最后一个元素 21 printf("最后一个元素是: %s \n",range.last()) 22 23 //是否包含某个元素 24 printf("是否包含 7 , %s \n",range.contains(7)) 25 26 27 //以下两个方法必须重写,否者会抛异常 28 //按下标删除元素 29 //range.remove(1) 30 //println(range) 31 32 //清空列表 33 //range.clear() 34 //println(range.size())
输出:
1 1..5 2 >>1 3 >>2 4 >>3 5 >>4 6 >>5 7 1 8 2 9 3 10 4 11 5 12 长度 : 5 13 下标1的元素是: 2 14 最后一个元素是: 5 15 是否包含 7 , false
17,闭包
闭包就是能够读取其他函数内部变量的函数。
1 //有参数 2 def sayHi = { 3 sb -> printf("hi , %s \n", sb) 4 } 5 6 // 无参数 7 def myname = { 8 println("my name is bob") 9 } 10 11 sayHi.call("tom") 12 sayHi("tom") 13 sayHi "tom" 14 myname() 15 16 def say = { 17 name, fun, age -> 18 printf("name: %s \n" ,name) 19 fun() 20 printf("age: %s \n" ,age) 21 22 } 23 24 // 函数调用可以省略圆括号() 25 say "tom", { 26 println("hehe") 27 }, 12 28 29 30 def list = [1,2,3] 31 // 闭包会默认传入一个参数 it 和 java 的this 一样,代表当前对象 32 list.each { 33 println(it) 34 }
输出:
1 hi , tom 2 hi , tom 3 hi , tom 4 my name is bob 5 name: tom 6 age: 12 7 1 8 2 9 3
End