class Robot { def type, height,width def access(Map location, weight, fragile){ println “Received fragile? f r a g i l e , w e i g h t : fragile, weight: fragile,weight:weight, loc:$location” }
static void main(args) { Robot robot = new Robot(type: 'arm', width: 10, height: 40) println "$robot.type, $robot.height, $robot.width" robot.access(x:30, y:20, z:10, 50, true) // 可以修改参数顺序 robot.access(50, true, x:30, y:20, z:10) }}
Groovy会把末尾的数组行参视作可选的,可以为最后一个行参提供0个或多个值