Groovy: 比JavaBean好用的GroovyBean

[size=x-large]Groovy: 比JavaBean好用的GroovyBean[/size]

在Java里我们经常使用JavaBean,GroovyBean用更简单的语法提供了JavaBean一样的功能。 我们在JavaBean里定义一个属性时需要自己添加get/set方法。虽然现在的大多数IDE都可以为我们自动生成这些方法,但是在属性很多的情况下还是这些方法还是会影响程序的可读性。

下面我们来先看一个GroovyBean的例子:

class Car {
int numberOfDoors
String model
String brand
boolean automatic
double price

String toString() {
"[Car details => brand: '${brand}', model: '${model}', #doors: '${numberOfDoors}', automatic: '${automatic}', price: '${price}']"
}
}



然后我们就可以在其他的Groovy类或脚本里这样使用:


Car ford = new Car(brand: 'Ford', model: 'Focus', numberOfDoors: 4, automatic: false, price: 24995)
Car toyota = new Car(brand: 'Toyota', model: 'Verso')
toyota.automatic = true // 是的,这里其实是在调用setAutomatic.
toyota.setPrice(28919) // 和Java的set方法一样使用
toyota.setNumberOfDoors 5 // 因为自由一个参数,在Groovy中可以不够用括号
println ford // 输出: [Car details => brand: 'Ford', model: 'Focus', #doors: '4', automatic: 'false', price: '24995.0']
println toyota // 输出: [Car details => brand: 'Toyota', model: 'Verso', #doors: '5', automatic: 'true', price: '28919.0']


这里的Car类就是一个GroovyBean。看上去我们好像只定义了一些字段,但是因为我们没有加入任何的访问修饰(public, protected 或private) 这里的字段实际上在编译时会变成属性。Groovy会自动为这些方法生产get/set方法,所以我们可以像JavaBean的属性一样使用。如果想要了解详细的编译规则看这里http://groovy.codehaus.org/Groovy+Beans 。通过下面的Java程序(注意是Java程序,不是Groovy程序)我们可以证明Car类实际上是一个JavaBean。


public class CarApp {
public static void main(String[] args) {
Car car = new Car();
car.setNumberOfDoors(3);
car.setModel("A3");
car.setBrand("AUDI");
car.setPrice(32010);
System.out.println(car); // 输出: [Car details => brand: 'AUDI', model: 'A3', #doors: '3', automatic: 'false', price: '32010.0']
}
}


GroovyBean非常灵活,如果我们要让一个property是只读的,只要把它声明为final。我们把brand声明为final,运行上面的Groovy脚本,就会得到下面的异常:

Caught: groovy.lang.ReadOnlyPropertyException: Cannot set readonly property: brand for class: Car


我们可以根据需要添加自己的get/set方法。例如我们把model属性的set方法声明为protected。

class Car {
int numberOfDoors
String model
final String brand // Readonly, public getter is generated, no setter.
boolean automatic
double price

// Protected setter, public getter还是会自动生成.
protected void setModel(modelName) {
this.model = modelName
}

String toString() {
"[Car details => brand: '${brand}', model: '${model}', #doors: '${numberOfDoors}', automatic: '${automatic}', price: '${price}']"
}
}


要想直接方法GroovyBean中的字段,我们可以用.@运算符。用这个运算符,我们可以越过get/set方法。当然一般我们不会这么做,因为这正是我们使用属性的原因(一般只有在类的内部使用)。不管怎么我们还是看看怎么用吧:


class Car {
int numberOfDoors
String model
String brand
boolean automatic
double price

public void setBrand(brand) {
this.brand = brand + ' set via setter method'
}

String toString() {
"[Car details => brand: '${brand}', model: '${model}', #doors: '${numberOfDoors}', automatic: '${automatic}', price: '${price}']"
}
}



Car ford = new Car(brand: 'Ford', model: 'Focus', numberOfDoors: 4, automatic: false, price: 24995)
println ford.brand // 输出: Ford set via setter method (这里通过get方法)

ford.@brand = 'Overrule brand'
println ford.brand // 输出: Overrule brand (这里通过get方法)
println ford.@brand // 输出: Overrule brand (这里直接方法字段)


GroovyBean提供了一种比JavaBean更加简洁的属性访问机制。


http://mrhaki.blogspot.com/2009/08/groovy-goodness-groovybeans-simpler.html
发布了15 篇原创文章 · 获赞 2 · 访问量 1万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章