mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-27 09:15:50 +03:00
Optional parameters in methods
This commit is contained in:
parent
f0f161981f
commit
ec1090d7a8
@ -180,6 +180,21 @@ class Foo {
|
|||||||
def lastName
|
def lastName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Methods with optional parameters
|
||||||
|
*/
|
||||||
|
|
||||||
|
// A mthod can have default values for parameters
|
||||||
|
def say(msg = 'Hello', name = 'world') {
|
||||||
|
"$msg $name!"
|
||||||
|
}
|
||||||
|
|
||||||
|
// It can be called in 3 different ways
|
||||||
|
assert 'Hello world!' == say()
|
||||||
|
// Right most parameter with default value is eliminated first.
|
||||||
|
assert 'Hi world!' == say('Hi')
|
||||||
|
assert 'learn groovy' == say('learn', 'groovy')
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Logical Branching and Looping
|
Logical Branching and Looping
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user