Created
January 23, 2009 17:16
-
-
Save fogus/51092 to your computer and use it in GitHub Desktop.
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
// Example for http://gist.github.com/51089 | |
public class AsteroidTest { | |
class Asteroid {} | |
class Spaceship {} | |
@Multi("collide") | |
public void collideOO(Object X, Object Y) { | |
log("?? Bang, what happened? ", X, Y); | |
} | |
@Multi("collide") | |
public void collideAA(Asteroid X, Asteroid Y) { | |
log("AA Look at the beautiful fireworks! ", X, Y); | |
} | |
@Multi("collide") | |
public void collideAS(Asteroid X, Spaceship Y) { | |
log("AS Is it fatal? ", X, Y); | |
} | |
@Multi("collide") | |
public void collideSA(Spaceship X, Asteroid Y) { | |
log("SA Is it fatal? ", X, Y); | |
} | |
@Multi("collide") | |
public void collideSS(Spaceship X, Spaceship Y) { | |
log("SS Who's fault was it? ", X, Y); | |
} | |
@Multi("collide") | |
public void collide1S(String X, Spaceship Y) { | |
log("1S any string? ", X, Y); | |
} | |
@Multi("collide") | |
public void collide2S(@V("hi") String X, Spaceship Y) { | |
log("2S 'hi' value? ", X, Y); | |
} | |
protected Multimethod mm = new Multimethod("collide", getClass()); | |
public void collide(Object X, Object Y) { | |
mm.invoke(this, X, Y); | |
} | |
public void run() { | |
Object A = new Asteroid(); | |
Object S = new Spaceship(); | |
collide(A, A); | |
collide(A, S); | |
collide(S, A); | |
collide(S, S); | |
collide(A, 1); | |
collide(2, A); | |
collide(S, 3); | |
collide(4, S); | |
collide(5, null); | |
collide(null, null); | |
collide("hi", S); | |
collide("hello", S); | |
} | |
public void log(Object... args) { | |
for(Object o: args) { | |
if(o instanceof String) { | |
System.out.print(" " + (String) o); | |
} else { | |
System.out.print(" " + o); | |
} | |
} | |
System.out.println(); | |
} | |
public static void main(String[] args) throws Exception { | |
AsteroidTest t = new AsteroidTest(); | |
t.run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment