前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Groovy中 使用Tap方法轻松创建对象

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

原创
作者头像
白石
修改2019-08-22 14:33:18
1.6K0
修改2019-08-22 14:33:18
举报
文章被收录于专栏:白石白石

使用Tap方法轻松创建对象

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

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

代码语言:txt
复制
/**
 * 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方法来演示使用不同参数值的多个调用的差异:

代码语言:txt
复制
/**
 * 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

代码语言:txt
复制
/**
 * 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 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 使用Tap方法轻松创建对象
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档