On Github dmies / hello-groovy
von Daniel Mies
public class Greeter { private String owner; public String getOwner() { return owner; } public void setOwner(String owner) { this.owner = owner; } public String greet(String name) { return "Hello " + name + ", I'm " + owner; } public static void main(String... args){ Greeter greeter = new Greeter(); greeter.setOwner("Max"); System.out.println(greeter.greet("Moritz")); } }
public class Greeter { private String owner; public String getOwner() { return owner; } public void setOwner(String owner) { this.owner = owner; } public String greet(String name) { return "Hello " + name + ", I'm " + owner; } public static void main(String... args){ Greeter greeter = new Greeter(); greeter.setOwner("Max"); System.out.println(greeter.greet("Moritz")); } }
import java.lang.* import java.util.* import java.net.* import java.io.* import java.math.BigInteger import java.math.BigDecimal import groovy.lang.* import groovy.util.*
In Groovy können einige Dinge weggelassen werden:
class Greeter { private String owner String getOwner() { owner } void setOwner(String owner) { this.owner = owner } String greet(String name) { "Hello " + name + ", I'm " + owner } static void main(String... args){ def greeter = new Greeter() greeter.setOwner("Max") System.out.println greeter.greet("Moritz") } }
class Greeter { String owner String greet(String name) { "Hello ${name}, I'm ${owner}" } } def greeter = new Greeter(owner: "Max") // greeter.owner = "Max" // greeter["owner"] = "Max" println greeter.greet("Moritz")
def adder = {a,b -> a+b}
def list = [1, 2, 3]
def map = [a:1, b:2, c:3]
def regex = ~/.*foo.*/
def range = 'a'..'z'
def adder = {a,b -> a+b} // Closure adder = ... assert adder.call(1,2) == 3 assert adder('a','b') == 'ab'
def doubler = {it * 2} assert doubler(2) == 4
def sum = {... elements -> elements.sum()} assert sum(1, 2, 3) == 6 assert sum('c', 'g', 'm') == 'cgm'
def intAdder = {int a, int b = 4 -> a + b} assert intAdder(1, 2) == 3 assert intAdder(1) == 5
def printer = System.out.&println printer "Hallo"
@groovy.transform.Immutable class Person { String name Integer age } def persons = [ new Person('Guillaume', 36), new Person('Marion', 5), new Person('Erine', 1)] def names = persons.findAll { it.age < 18 } .collect { it.name.toUpperCase() } .sort() .join(', ') assert names == 'ERINE, MARION'
void ifNot(boolean cond, Closure c) { if (!cond) c() } def printer = System.out.&println ifNot(10 < 9) { printer "10 is not < 9" } // ifNot((10 < 9){...})
def list = ['a', 'b', 'c'] list << 'd' assert list.contains('d') assert list.findAll { it.startsWith 'a' }.size() == 1 assert list.collect { it.toUpperCase() } == ['A', 'B', 'C', 'D'] assert list.inject('') { a, b -> a + b } == 'abcd'
def map = [name: 'Guillaume', age: 36] map.daughters = ['Marion', 'Erine'] assert map['daughters'].contains('Marion')
def range = 'a'..'z' assert range.contains('m') assert range.contains('z') def exclusive = 1..<10 assert !exclusive.contains(10) def reverse = 10..0 assert reverse[0] == 10 assert reverse[-1] == 0
def name = 'Groovy' def tmpl = """ Dear Mr ${name}, You're the winner of the lottery! Yours sincerly, """ assert tmpl.toString().contains('Groovy')
trait FlyingAbility{ String fly(){ "I'm flying" } } class Bird implements FlyingAbility{} def bird = new Bird() assert bird.fly() == "I'm flying"
trait Named{ String name } class Bird implements Named{} def bird = new Bird(name: "Colibri") assert bird.name == "Colibri"
trait Named{ String name } trait FlyingAbility extends Named{ String fly(){ "I'm a flying ${name}" } } class Bird implements FlyingAbility{} def bird = new Bird(name: "Colibri") assert bird.name == "Colibri" assert bird.fly() == "I'm a flying Colibri"
trait Named{ String name } trait FlyingAbility{ String fly(){ "I'm a flying ${name}" } } class Bird implements FlyingAbility, Named{} def bird = new Bird(name: "Colibri") assert bird.name == "Colibri" assert bird.fly() == "I'm a flying Colibri"
trait KiteSurfer{ String surf() { "kite" } } trait WebSurfer{ String surf() { "web" } } class Hipster implements KiteSurfer, WebSurfer{} def hipster = new Hipster() assert hipster.surf() == "web"
trait KiteSurfer{ String surf() { "kite" } } trait WebSurfer{ String surf() { "web" } } class Hipster implements KiteSurfer, WebSurfer{ String surf() { KiteSurfer.super.surf() } } def hipster = new Hipster() assert hipster.surf() == "kite"
trait Named{ String name } class Animal{} def bird = new Animal() as Named bird.name = "Colibri" assert bird.name == "Colibri"
trait Named{ String name } trait Quacks{ String quack() { "quack" } } class Animal{} def bird = new Animal().withTraits Named, Quacks bird.name = "Donald" assert bird.name == "Donald" assert bird.quack() == "quack"