Groovy中 使用Tap方法轻松创建对象

使用Tap方法轻松创建对象

Groovy 2.5.0将tap方法添加到所有对象并更改with方法的方法签名。 在上一篇文章 中,我们已经了解了with方法。在Groovy 2.5.0中,我们可以为with方法添加一个额外的boolean参数。 如果值为false(默认值),则with方法必须返回与闭包调用返回的值相同的值。如果值为true,则返回调用with方法的对象实例。 新的tap方法是with(true)的别名,所以它总是返回对象实例。

在第一个例子中,我们使用tap方法创建一个新的Sample对象并设置属性值并调用Sampleclass的方法:

/**
 * 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编写。

转载于:https://my.oschina.net/wstone/blog/3094468

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值