Difference between revisions of "Groovy"

From no name for this wiki
Jump to: navigation, search
(Hello Worlds)
(Hello Worlds)
 
(One intermediate revision by the same user not shown)
Line 8: Line 8:
 
  x = "Claude"
 
  x = "Claude"
 
  print "Hallo ${x}"  
 
  print "Hallo ${x}"  
 +
 +
Arrays:
 +
x = [1,2,3,4]
 +
print x[3]
 +
x[10] = 3
  
 
Maps:
 
Maps:
Line 23: Line 28:
  
 
== Classes  ==
 
== Classes  ==
 +
Einfaches Sample:
 +
public class Cat {
 +
  private int age = 3;
 +
 +
  def void printAge(){
 +
    println age;
 +
  }
 +
}
 +
Cat myCat = new Cat()
 +
myCat.printAge()
  
  

Latest revision as of 11:35, 18 June 2008

Um die Groovy Konsole zu starten, klicke hier: Groovy Konsole.


Hello Worlds

print "Hello World"

Marken in Strings:

x = "Claude"
print "Hallo ${x}" 

Arrays:

x = [1,2,3,4]
print x[3]
x[10] = 3

Maps:

x = [vorname:"hugo", nachname:"boss"]
print x.vorname

Closures

Simple Klammer. it ist defaultmässig der Parameter.

myclosure = {it + it}
myclosure(4)

Klammer mit zwei Paremetern:

myprint = {first, last -> print "[" + first + ", " + last + "]"}
myprint("hugo","boss")

Classes

Einfaches Sample:

public class Cat {
  private int age = 3;

  def void printAge(){
   println age;
  }
}
Cat myCat = new Cat()
myCat.printAge()


Interfaces

Implementation eines einfachen Interfaces:

x = {print "hello"} as Runnable
x.run()

Definition und Implementation eines Interfaces mit mehreren Methoden:

interface X { void f1(); void f2(int n) }
x = {Object[] args -> println "method called with $args"} as X
x.f1()
x.f2(1)

Der Output ist:
method called with null
method called with {1}

Interfaceimplementation als Map:

interface X { void f1(); void f2(int n) }
impl = [
  f1: {println "f1 called"},
  f2: { n -> println "f2 called. n:" + n}
]
implInst = impl as X
implInst.f1()
implInst.f2(5)