Skip to content

Instantly share code, notes, and snippets.

@fumokmm
Created July 29, 2012 05:06
Show Gist options
  • Save fumokmm/3196189 to your computer and use it in GitHub Desktop.
Save fumokmm/3196189 to your computer and use it in GitHub Desktop.
interface Cat {
String mew()
}
class Zash implements Cat {
String mew(){ "雑種" }
}
class Nora implements Cat {
String mew(){ "野良" }
}
def scottish = [mew:{"すこてぃっしゅ"}] as Cat
Cat.metaClass {
def 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()
// output ↓
// [Cat] 雑種
// [Cat] 野良
// [Cat] すこてぃっしゅ
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment