-
Notifications
You must be signed in to change notification settings - Fork 19
/
methods.groovy
41 lines (32 loc) · 919 Bytes
/
methods.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import groovy.transform.*
@Canonical class Dragon {
String name
def fly() {println "$name flying"}
// default values:
def say(String text = "I hear you") {println text}
}
Dragon dragon = new Dragon('Smaug');
dragon.fly(); // dragon is the object, and fly is the method
// java-style methods
public void fly() {
// flying code
}
public void fly(int x, int y) {
// fly to that x, y coordinate.
}
// java-style
public Dragon makeDragonNamed(String name) {
return new Dragon(name);
}
//groovy-style
def makeDragonNamed(name) {
new Dragon(name)
}
println(makeDragonNamed("Norbert"))
void printSpaced(Object... objects) {
for (Object o : objects) System.out.print(o + " ");
}
printSpaced("A", "B", "C"); // A B C
println()
printSpaced(1, 2, 3); // 1 2 3
println()