Groovy 快速入門


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM