Last active
December 15, 2015 05:29
-
-
Save timmolderez/5209524 to your computer and use it in GitHub Desktop.
Alternate version of this xkcd comic: http://xkcd.com/1188
This file contains 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
/** | |
* Alternate version of this xkcd comic: http://xkcd.com/1188 | |
* | |
* My inner nitpicker was bugged by the fact that the comic isn't actually | |
* using the throw-catch exception mechanism to throw the ball over to the | |
* target, which I thought was the whole point. | |
* What actually happens is this: the parent throws the ball, catches it | |
* himself and then just passes it to the child (as a parameter)... which looks | |
* kinda silly when picturing it :) So I had to rewrite the thing such that | |
* it's a proper game of play catch.. | |
*/ | |
class Ball extends Throwable {} | |
class Person{ | |
public Person target = null; | |
public Ball ball = null; | |
public void giveMeYourBestShot() { | |
try { | |
target.hereItComes(); | |
} catch (Ball b) { | |
ball = b; | |
target.giveMeYourBestShot(); | |
} | |
} | |
public void hereItComes() throws Ball { | |
Ball b = ball; | |
ball = null; | |
throw b; | |
} | |
public static void main(String[] args) { | |
Person parent = new Person(); | |
Person child = new Person(); | |
parent.target = child; | |
child.target = parent; | |
parent.ball = new Ball(); | |
child.giveMeYourBestShot(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment