Groovy 2.5.0将tap
方法添加到所有对象并更改with
方法的方法签名。 在上一篇文章 中,我们已经了解了with
方法。在Groovy 2.5.0中,我们可以为with
方法添加一个额外的boolean
参数。 如果值为false
(默认值),则with
方法必须返回与闭包调用返回的值相同的值。如果值为true
,则返回调用with
方法的对象实例。 新的tap
方法是with(true)
的别名,所以它总是返回对象实例。
在第一个例子中,我们使用tap
方法创建一个新的Sample
对象并设置属性值并调用Sample
class的方法:
/**
* Sample class with some properties
* and a method.
*/
class Sample {
String username, email
List<String> labels = []
void addLabel(value) {
labels << value
}
}
// Use tap method to create instance of
// Sample and set properties and invoke methods.
def sample =
new Sample().tap {
assert delegate.class.name == 'Sample'
username = 'mrhaki'
email = 'email@host.com'
addLabel 'Groovy'
addLabel 'Gradle'
// We use tap, an alias for with(true),
// so the delegate of the closure,
// the Sample object, is returned.
}
assert sample.labels == ['Groovy', 'Gradle']
assert sample.username == 'mrhaki'
assert sample.email == 'email@host.com'
在下面的示例中,我们使用with
方法来演示使用不同参数值的多个调用的差异:
/**
* Sample class with some properties
* and a method.
*/
class Sample {
String username, email
List<String> labels = []
void addLabel(value) {
labels << value
}
}
// Use with method to create instance of
// Sample and set properties and invoke methods.
def sample1 =
new Sample().with {
assert delegate.class.name == 'Sample'
username = 'mrhaki'
email = 'email@host.com'
addLabel 'Groovy'
addLabel 'Gradle'
}
// With method returns the result
// from the closure. In the previous
// case the return result is null,
// because the last statement addLabel
// is used as return value. addLabel has
// return type void.
assert !sample1
// Use with method to create instance of
// Sample and set properties and invoke methods.
def sample2 =
new Sample().with {
assert delegate.class.name == 'Sample'
username = 'mrhaki'
email = 'email@host.com'
addLabel 'Groovy'
addLabel 'Gradle'
// Explicitly return delegate of
// closure, which is the Sample object.
return delegate
}
assert sample2.labels == ['Groovy', 'Gradle']
assert sample2.username == 'mrhaki'
assert sample2.email == 'email@host.com'
// Use with method to create instance of
// Sample and set properties and invoke methods.
def sample3 =
new Sample().with(true) {
assert delegate.class.name == 'Sample'
username = 'mrhaki'
email = 'email@host.com'
addLabel 'Groovy'
addLabel 'Gradle'
// We use with(true), so the
// delegate of the closure, the Sample
// object, is returned.
}
assert sample3.labels == ['Groovy', 'Gradle']
assert sample3.username == 'mrhaki'
assert sample3.email == 'email@host.com'
使用with
方法的一个很好的用例是使用来自对象的值将对象转换为另一种类型。 在下一个例子中,我们使用来自Sample
对象的值来创建一个新的String
:
/**
* Sample class with some properties
* and a method.
*/
class Sample {
String username, email
List<String> labels = []
void addLabel(value) {
labels << value
}
}
def sample =
new Sample().tap {
username = 'mrhaki'
email = 'email@host.com'
addLabel 'Groovy'
addLabel 'Gradle'
}
// The with method can be very useful to
// transform object to another type using
// values from the object.
def user = sample.with { "$username likes ${labels.join(', ')}." }
assert user == 'mrhaki likes Groovy, Gradle.'
用Groovy 2.5.0编写。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。