Skip to content

Instantly share code, notes, and snippets.

@uilian
Created November 17, 2014 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uilian/35837ecf644a00f89036 to your computer and use it in GitHub Desktop.
Save uilian/35837ecf644a00f89036 to your computer and use it in GitHub Desktop.
Sample Groovy
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