Groovy 安装与入门
windows下的安装,
配置Path和GROOVY_HOME环境变量。
打开cmd,如下,
C:\Users\Lenovo>groovy -version Groovy Version: 2.4.3 JVM: 1.8.0_20 Vendor: Oracle Corporation OS: Windows 7 C:\Users\Lenovo>
这样groovy就可以使用了。
以下是groovy的脚本
package com.usoft def func(a, b) { println("Your name is: " + a + " and your age is: " + b); } func("Diego", 30) def func1(a, b = 25) { if (b == null) { println("Your name is: " + a + " and your age is a secret"); } else { println("Your name is: " + a + " and your age is: " + b); } } func1("Diego") func1("Diego", 30) func1("Diego", null)
运行输出,
Your name is: Diego and your age is: 30
Your name is: Diego and your age is: 25
Your name is: Diego and your age is: 30
Your name is: Diego and your age is a secret
其中func1 方法有一个参数为默认值。写groovy的方法就和写javascript方法一样,变量的声明没有类型。
package com.usoft class Person { def name def age def weight def height def info() { println "我是$name,我的体重是$weight" } static void main(args) { def person = new Person(name: "hello person", age: 12, weight: 1234, height: 1234455) println person.name println person.age println person.weight println person.height println "调用方法info" person.info() } }
运行并输出,
hello person
12
1234
1234455
调用方法info
我是hello person,我的体重是1234
groovy中默认的修饰符就是public,所以public修饰符你根本就不需要写
groovy中没有语句结束符,当然为了与java保持一致性,你也可以使用 ; 号作为语句结束符
map集合,如下代码示例,
package com.usoft def map = ['name': 'test groovy', 'age': 13, 'sex': 'boy'] // 集合的相加操作,就是合并两个map集合 map = map + ['weight': 123] map.put('weight', 123) map.put('height', 123) // 添加元素 map.father = 'Keeee' // 访问元素 println map['father'] println map.name def number = [1, 3, 4, 43, 32] println number[2] //添加元素 left shift number << 345676 println number[5] //打印数组的元素 //定义一个空集合 def m = [:] //设置集合的内容,其中name,age为key m.name = "Lucy" m.age = 16 m.height = 165 //这种方式也可以向map中添加元素 m.put("sex", "女") //取得集合的内容(单个) println m.get("name") m.each { it -> println it.key + "," + it.value } //删除集合元素 m.remove("height") println m.get("sex") println m.get("height") //null
这里注意有这么一个符号,<<,这个符号在Java里是位运算里的移位符号。那么在groovy里,这个符号的是表示给集合或数组增加元素,类似java 集合操作的add方法,如下代码,
def list = ['a', 'b', 'c'] list.each { println it } println '-----------------------' list << 'e' list << 'f' list << "haha" list.each { println it }
list集合代码如下,
def apples = [] // 定义一个空集合 println(apples) //[] apples << 'e' apples << 'f' apples << "haha" apples << 110 apples << 111 apples.each { println it } //通过下标访问list的元素 println("apples[1]=" + apples[1]) //下标为 1 //下标 -2 ,也就是倒数第二个元素 println("apples[-2]=" + apples[-2])
Ranges allow you to create a list of sequential values. These can be used as List since Range extends java.util.List.
Ranges defined with the .. notation are inclusive (that is the list contains the from and to value).
Ranges defined with the ..< notation are half-open, they include the first value but not the last value.
代码示例,
// an inclusive range def number_range = 5..8 assert number_range.size() == 4 assert number_range.get(2) == 7 assert number_range[2] == 7 assert number_range instanceof java.util.List assert number_range.contains(5) assert number_range.contains(8) // lets use a half-open range number_range = 5..<8 assert number_range.size() == 3 assert number_range.get(2) == 7 assert number_range[2] == 7 assert number_range instanceof java.util.List assert number_range.contains(5) assert !number_range.contains(8) //get the end points of the range without using indexes number_range = 1..10 assert number_range.from == 1 assert number_range.to == 10
You can iterate on a range using a classic for loop:
for (i in 1..10) { println "Hello ${i}" }
but alternatively you can achieve the same effect in a more Groovy idiomatic style, by iterating a range with each method:
(1..10).each { i -> println "Hello ${i}" }
Ranges can be also used in the switch statement:
switch (years) { case 1..10: interestRate = 0.076; break; case 11..25: interestRate = 0.052; break; default: interestRate = 0.037; }
// groovy ranges 和 list 集合 myList = ['a', 'b', 'c', 'd', 'e', 'f'] assert myList[0..2] == ['a', 'b', 'c'] assert myList[0, 2] == ['a', 'c'] //replace myList[0..2] = ['x', 'y', 'z'] assert myList == ['x', 'y', 'z', 'd', 'e', 'f'] //remove myList[0..2] = [] assert myList == ['d', 'e', 'f'] //insert or replace myList[2..2] = ['f', 'x'] assert myList == ['d', 'e', 'f', 'x']
def x = 1 def gs = "x = ${x}" def gs_2 = "x = $x" x = 2 println(x) println(gs) println(gs_2)
groovy 定义字符串,代码如下,单行字符串和多行字符串
def winpathSlashy = /C:\windows\system32/ println winpathSlashy println winpathSlashy.class.getName() def name = "vishal" def path = "c:/groovy/" def multilineslashy = / Hello $name path $path dollar = $ path = c:\/groovy / // multilineslashy 字符串相当于是一个模板字符串 println multilineslashy println multilineslashy.class.getName()
运行输出如下,
C:\windows\system32
java.lang.String
Hello vishal
path c:/groovy/
dollar = $
path = c:/groovy
org.codehaus.groovy.runtime.GStringImpl
可以看到字符串类型的类型在这里有两种,一个是java.lang.String,另一个是GStringImpl。GStringImpl是groovy自己实现的字符串类型,他可以实现模板类型的字符串。
===================END===================