Skip to content

Instantly share code, notes, and snippets.

@fumokmm
Created July 12, 2012 22:04
Show Gist options
  • Save fumokmm/3101384 to your computer and use it in GitHub Desktop.
Save fumokmm/3101384 to your computer and use it in GitHub Desktop.
overriding interface method on Groovy
interface Cat {
String mew()
}
class Zash implements Cat {
String mew(){ "雑種" }
}
class Nora implements Cat {
String mew(){ "野良" }
}
def scottish = [mew:{"すこてぃっしゅ"}] as Cat
Cat.metaClass.define {
oldMew = Cat.metaClass.getMetaMethod('mew', [] as Class[])
mew = {->
"[Cat] ${oldMew.invoke(delegate, null)}"
}
}
Cat[] cats = [new Zash(), new Nora(), scottish] as Cat[]
for (c in cats) println c.mew()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment