Groovy之面向对象与泛型

[TOC]

面向对象

变量声明范例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class SomeClass {
public fieldWithModifier
String typedField
def untypedField
protected filed1, filed2, filed3

static ClassFiled

public static final String CONSTA = 'a', CONSTB = 'b'

def someMethod(){
def localUntypedVar = 1
int localTyoedVar = 1
def localVarWithoutAssignment, andAnotherOne
}
}

def localVar = 1 //脚本声明变量
boundVar = 1

def someMthod(){
localMethodVar = 1
boundVar2 = 1
}


1
2
3
4
5
6
7
8
9
10
11
//定位字段
class Counter{
public count = 0
}

def counter = new Counter()
counter.count = 1
assert counter.count == 1
def fieldNam = 'count'
counter[fieldName] = 2
assert counter['count'] == 2

方法声明

方法返回值声明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class SomeClass{
static void main(args){
def some = new SomeClass()
assert 'hi' == some.publicUntyedMethod()
assert 'ho' == some.publicTypedMethod()
combineMethod()
}

void publicVoidMethod(){}
def pubicVoidMethod(){
return 'hi'
}
String publicTypedMethod(){
return 'ho'
}
protected static final void combineMethod(){}
}

方法参数声明

1
2
3
4
5
6
7
8
9
10
11
12
13
class SomeClass{
static method(arg){
println 'untyped'
}

static method(String arg){
println 'typed'
}

static method(arg1, Number arg2){
println 'mixed'
}
}

高级方法参数使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Summer{
def sumWithDefaults(a,b,c = 0){
return a + b + cs
}

def sumWithList(List args){
return args.inject(0){
sum,i ->
sum += i
}
}

def sumWithOptionals(a, b, object[] optionals){
return a + b + sumWithList(optionals.toList())
}

def sumNamed(Map args){
['a','b','c'].each{
args.get(it,0)
}
return args.a + args.b + args.c
}
}

构造函数

位置参数

1
2
3
4
5
6
7
8
9
10
11
12
class VendorWithCtor {
String name, product

VendorWithCtor(name,product){
this.name = name
this.product = product
}
}

def first = new VendorWithCtor('canoo','ULC') // Normal constructor use
def second = ['Canoo', 'ULC'] as VendorWithCtor // Coercion with as
VendorWithCtor third = ['Canoo', 'ULC'] // Corecion in assignment

命名参数

1
2
3
4
5
6
7
8
9
10
class Vendor{
String name, product
}
new Vendor()
new Vendor(name: 'Canoo')
new Vendor(product: 'ULC')
new Vendor(name: 'Canoo', product: 'ULC')

def vendor = new Vendor(name: 'Canoo')
assert 'Canoo' == vendor.name

隐式构造函数

1
2
3
4
java.awt.Dimension area
area = [200,100]
assert area.width == 200
assert area.height == 100

属性获取设置器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class MyBean{
def a
def b

def getA(){
return a
}

def getB(){
return b
}

def setA(a){
this.a = a
}

def setB(b){
this.b = b
}
}

def mb = new MyBean()
mb.a = 10
mb.b = 30

属性获取方法,Groovy方式直接.属性名就可以了.

Java Groovy
getPropertyName propertyName
setPropertyName(value) propertyName = value

属性获取器和@语法的使用区别

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class MrBean{
String firstName, lastName
String getName(){
return "$firstName $lastName"
}
}

def bean = new MrBean(firstName: 'Rowan')
bean.lastName = 'Atkinson'

//advanced accessors with groovy

class DoubleBean{
public value //visible value

void setValue(value){
this.value = value //inner field access
}

def getValue(){
value * 2 //inner field access
}
}

def bean2 = new DoubleBean(value: 100)
assert 200 == bean2.value //Property access use getter method
assert 100 == bean2.@value // Outer field access directly access

GPaths查询对象

invoke example for Gpath

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Book {
String name
List authors
}
class Author {
String name
Address addr
}
class Address {
String province
String city
}

def addr1 = new Address(province:'Guangdong',city:'Shenzhen')
def addr2 = new Address(province:'Gunagdong',city:'Guangzhou')
def addr3 = new Address(province:'Hunan',city:'Changsha')
def addr4 = new Address(province:'Hubei',city:'Wuhan')
def books = [new Book(name:'A glance at Java',authors:[new Author(name:'Tom',addr:addr1)]),
new Book(name:'Deep into Groovy',authors:[new Author(name:'Tom',addr:addr1),new Author(name:'Mike',addr:addr3)]),
new Book(name:'A compare of Struts and Grails',authors:[new Author(name:'Wallace',addr:addr4),new Author(name:'Bill',addr:addr2)]),
new Book(name:'learning from Groovy to Grails',authors:[new Author(name:'Wallace',addr:addr3)])]

//目标是找到作者是"Tom"的书籍的书名
//Java风格
def booksOfTomOldWay = []
books.each {
def book = it
def aus = it.authors

aus.each {
au->
if (au.name == 'Tom')
booksOfTomOldWay << book.name
}
}

println booksOfTomOldWay
//Groovy风格 非常简练,也符合一个正常的思考顺序.

def booksOfTom = books.grep{
it.authors.any{
it.name == 'Tom'
}
}.name

println booksOfTom

空操作安全

1
2
Duck duck = new Duck()
duck?.eat() // ?.标识如果对象是一个空值,那么久不调用函数.只有非空对象才会执行函数的调用