Created
November 17, 2014 11:44
-
-
Save uilian/35837ecf644a00f89036 to your computer and use it in GitHub Desktop.
Sample Groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Animal implements Skills, Talk { | |
abstract String voice | |
Animal(){ | |
initSkills() | |
} | |
} | |
trait Talk { | |
def talk(){println "Ele diz ' ${voice.concat(' ') * 3}'"} | |
} | |
trait CanFly { | |
def fly(){println 'Sabe voar...'} | |
} | |
trait CanWalk { | |
def walk(){println 'Sabe andar...'} | |
} | |
trait CanSwim { | |
def swim(){println 'Sabe nadar...'} | |
} | |
trait Skills { | |
def skills = [] | |
def initSkills(){ | |
if (this instanceof CanFly) skills << 'voar' | |
if (this instanceof CanSwim) skills << 'nada' | |
if (this instanceof CanWalk) skills << 'andar' | |
} | |
def canDo(){ | |
skills.each(){println 'Ele sabe ' + it} | |
} | |
} | |
class Fish extends Animal implements CanSwim { | |
String voice = '...' | |
} | |
class Dog extends Animal implements CanWalk { | |
String voice = 'au' | |
} | |
class Duck extends Animal implements CanFly, CanWalk, CanSwim{ | |
String voice = 'quack' | |
} | |
def animal = new Duck() | |
animal.talk() | |
animal.canDo() | |
println '' | |
println '' | |
animal = new Dog() | |
animal.talk() | |
animal.canDo() | |
println '' | |
println '' | |
trait Chihuahua { | |
def humor(){println 'Humor: 50% Odio | 50% Tremor'} | |
} | |
def moody = animal as Chihuahua | |
moody.talk() | |
moody.canDo() | |
moody.humor() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment