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"
A Groovy class is a collection of data and the methods that operate on that data. Together, the data and methods of a class are used to represent some real world object from the problem domain. A class in Groovy declares the state (data) and the behavior of objects defined by that class.
When a Groovy class definition declares a field without an access modifier, then a public setter/getter method pair and a private instance variable field is generated which is also known as "property" according to the JavaBeans specification.
The @Newify transformation annotation allows other ways to create a new instance of a class. We can use a new() method on the class or even omit the whole new keyword. The syntax is copied from other languages like Ruby and Python.
Groovy is a fully fledged object-oriented (OO) language supporting all of the OO programming concepts that are familiar to Java developers: classes, objects, interfaces, inheritance, polymorphism, and others.
You have the with
method, as described by the great Mr Haki
item1.with{
name = "hello"
weight = "175"
}
Yes, you can do it like this:
item1.metaClass.setProperties(item1, [name: "hello", weight: "175"])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With