在Groovy中分配对象属性的简写方法?

raf*_*ian 15 groovy class object variable-assignment

我使用这个约定创建Groovy对象...

Item item1 = new Item( name: "foo", weight: "150")
Run Code Online (Sandbox Code Playgroud)

...是否存在操作属性对象的简写约定?像这样的东西......

item1( name: "hello", weight: "175") //this does not work, btw ;-)
Run Code Online (Sandbox Code Playgroud)

...代替...

item1.name = "hello"
item1.weight = "175"
Run Code Online (Sandbox Code Playgroud)

Gro*_*eek 23

你有这种with方法,正如伟大的哈基先生所描述的那样

item1.with{
    name = "hello"
    weight = "175"
}
Run Code Online (Sandbox Code Playgroud)