July 2013
Intermediate to advanced
370 pages
8h 27m
English
Groovy gives us the flexibility to initialize a JavaBean class.
When constructing an object, simply give values for properties as comma-separated
name-value pairs. This is a post-construction operation if our class has a no-argument
constructor. We can also design our methods so they can take named arguments.
To take advantage of this feature, define the first parameter as a
Map. Let’s see these in action:
| GroovyForJavaEyes/NamedParameters.groovy | |
| | class Robot { |
| | def type, height, width |
| | def access(location, weight, fragile) { |
| | println "Received fragile? $fragile, weight: $weight, loc: $location" |
| | } |
| | } |
| | robot = new Robot(type: 'arm', width: 10, height: 40) |
| | println ... |
Read now
Unlock full access