2015年11月22日

Gradle(12)–– Groovy

本章會介紹Gradle的核心語法為Groovy,讓使用者更容易了解gradle工具使用,Groovy運行在JVM上面,
所以很容易與Java做整合利用。

  • 資料類型
Data type Wrapper type values
byte Byte 0
short Short 0
int Integer 0
long Logn 0L
float Float 0.0f
double Double 0.0d
char Character \u0000
boolean Boolean false
String Not Applicable null

資料型態跟Java相同,現在介紹如何宣告變數,

範例一、 宣告變數
def a1 = ‘ It is a sunday.’
def a2 = “It is a sunday.”
def a3 = “example1. ${a1}”
def a4 =/ This is                //斷航符號 /
              a
             book /

  • 動態型別(Dynamic typing)宣告

Groovy支援動態型別與靜態型別兩種,靜態型別通常會再編譯時檢查、記憶體最佳化等等工作。
動態型別表示你不知道宣告的函式的型態或回傳的資料類型是什麼,但是這種方式開發人員比較
有彈性宣告方式。當我宣告時,不會預設型別給宣告之變數,會決定變數型別是在執行的時候才
會決定要丟甚麼型別給他。

範例二、動態型別宣告
//(1) 宣告變數
def  var1
var1 = “It’s a book”
println var1.class       // java.lang.String
var1 = 10
println var1.class      // class java.lang.Integer

//(2) 宣告函式
def addition(a,b) 
addition(1,2)
addition(‘Jack’,‘Janet’)

 

  • 控制結構
    常用的控制語法有if、else、for、switch等等,在groovy也有相同的用法

(1) if-lelse
二元運算:

if( a<input){
    println input
}else{
    println a
}


三元運算
(a<input) ? input : a

(2) switch

switch(input)
{
    case Integer : println("Integer Matched"); break;
    case BigDecimal : println("BigDecimal Matched");break;
    case String : println("String Matched");break;
    default : println("Nothing Matched"); break;
}


(3) for

def fruit = ['apple','banana','strawberry']
for(String s : fruit){
    println s
}


(4) while

int count = 0
while(count < 5) {
     println count++
}

  • 集合

(1) Set
集合是一群沒有排序且不重複物件,set集合可以有Null的資料,常用方法有add, addAll, remove, or removeAll

def set1 = [1,2,3,4,5,6] as Set
def set2 = new HashSet(['a','b','c','d'])

set1.add(7)
set1.add([8,9])
print set1                  //[1, 2, 3, 4, 5, 6, 7, [8, 9],]

set2.remove('d')
println set2              // [a, b, c]

Set union = set1 + set2
println union            // [1, 2, 3, 4, 5, 6, 7, [8, 9], a, b, c]

(2)List

List是一組有順序並且可以有重覆資料的物件集合,

def list1 = ['a','d','c']
def list2 = [5,1,7,2,9,1] as list

println list1[1]              // d
println list1.get(1)         // d
println list2.sort()         //[1, 1, 2, 5, 7, 9]
println list1.reverse()    // [c, d, a]

(3) Map
Map是一種主鍵-值的集合,為java.util.HashMap.

Map ageMap = [Jack:20, MaraYa:30,Katy:16,Li:19,Herry:18] as Map
ageMap.each{key, value ->
   print "Name is "+key
    print "Age is "+ value
   print "\n"
}

ageMap.each{entry –>
    print "Name is "+entry.key
    print "Age is "+ entry.value
    print "\n"
}

//結果
// Name is JackAge is 20
// Name is MaraYaAge is 30
// Name is KatyAge is 16
// Name is LiAge is 19
// Name is HerryAge is 18

(4) Range
這是Groovy特有的集合物件,用來做連續數列的集合。

def range1 = 1..10
Range range2 = 'c'..'e'
range1.each { print it }    //12345678910
range1.any { it > 5 }
range1.every { it > 0 }

List list3 = range1.step(2)
print list1                   // [a, d, c]

 

  • Closure

Closurer可以當作函式來使用,但是不需要回傳值或是輸入值會自動回傳,

範例一、 加法closure

def add = { it + it}
add("Hi!")                      // Hi!Hi!
add()                            // null + null => NullPointerException

closure會將外部變數的值,自動配置成內部的值,變數名稱要相同

範例二、 外部變數參考

def name="bryant"
def sayHello = { println name + " hello "}
sayHello()                            // bryant hello


沒有留言:

張貼留言