在Groovy中赋值对象属性的方法? [英] Shorthand way for assigning object properties in Groovy?

查看:1670
本文介绍了在Groovy中赋值对象属性的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此约定创建Groovy对象...

 项目item1 =新项目(名称:foo :150)

...有操作属性对象的简写约定吗?像这样...

  item1(name:hello,weight:175)//这不工作,btw ;-) 

...而不是...

  item1.name =hello
item1.weight =175
使用方法,

/feedproxy.google.com/~r/mrhaki/~3/Vx6FGAEWt5I/groovy-goodness-with-method.html\">由伟大的Haki先生描述

  item1.with {
name =hello
weight =175
}


I create Groovy objects using this convention...

Item item1 = new Item( name: "foo", weight: "150")

...is there a shorthand convention for manipulating properties object? something like this...

item1( name: "hello", weight: "175") //this does not work, btw ;-)

...instead of...

item1.name = "hello"
item1.weight = "175"

解决方案

You have the with method, as described by the great Mr Haki

item1.with{
    name = "hello"
    weight = "175"
}

这篇关于在Groovy中赋值对象属性的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆