Created
July 12, 2012 22:04
-
-
Save fumokmm/3101384 to your computer and use it in GitHub Desktop.
overriding interface method on Groovy
This file contains hidden or 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
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