Java Reference
In-Depth Information
Here's a script using the
Person
class:
Person mrIncredible = new Person()
mrIncredible.firstName = 'Robert'
mrIncredible.setLastName('Parr')
assert 'Robert Parr' ==
"${mrIncredible.firstName} ${mrIncredible.getLastName()}"
Person elastigirl = new Person(firstName: 'Helen', lastName: 'Parr')
assert 'Helen Parr' == elastigirl.toString()
The script shows that you also get a default,
map-based
constructor, so called because it
uses the same
property:value
syntax used by Groovy maps.
This idiom is so common in Groovy that getter and setter methods anywhere in the
standard library are typically accessed with the property notation. For example,
Calen-
dar.instance
is used to invoke the
getInstance
method on the
Calendar
class.
Movingnowtocollections ofinstances, I'llstartwithranges,thenmovetolists,andfinally
look at maps.
B.4. Collections
Since J2SE 1.2, the Java standard library has included the collections framework. The
framework defines interfaces for lists, sets, and maps, and provides a small but useful set of
implementation classes for each interface, as well as a set of polymorphic utility methods
in the class
java.util.Collections
.
Groovy can use all of these collections but adds a lot:
• Native syntax for lists and maps
• A
Range
class
• Many additional convenience methods
I'll present examples of each in this section.