Skip to content

Instantly share code, notes, and snippets.

@jakecoffman
Created October 13, 2015 19:23
Show Gist options
  • Save jakecoffman/9ca198cfd0a4fb68039c to your computer and use it in GitHub Desktop.
Save jakecoffman/9ca198cfd0a4fb68039c to your computer and use it in GitHub Desktop.
// Main
IElement element = getElement()
printer(element)
// Classes
IElement getElement() {
// simulate not knowing the type of element at runtime
if (new Random().nextInt() % 2 == 0) {
return new Element1(var1: "hello")
} else {
return new Element2(var2: "world")
}
}
def printer(Element1 e1) {
println e1.var1
}
def printer(Element2 e2) {
println e2.var2
}
interface IElement {
}
class Element1 implements IElement {
String var1
}
class Element2 implements IElement {
String var2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment