Last active
August 29, 2015 13:56
-
-
Save patrickgombert/9165881 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
public static final class Ratio extends Number { | |
/* .. */ | |
} | |
public static final class Addition { | |
public static Number add(Integer x, Integer y) { | |
/* .. */ | |
} | |
public static Number add(Ratio x, Ratio y) { | |
/* .. */ | |
} | |
public static Number add(Ratio x, Integer y) { | |
/* .. */ | |
} | |
public static Number add(Number x, Number y) { | |
Class xClass = x.getClass(); | |
if (xClass == Integer.class) | |
x = (Integer) x; | |
else if (xClass == Ratio.class) | |
x = (Ratio) x; | |
Class yClass = y.getClass(); | |
if (yClass == Integer.class) | |
y = (Integer) y; | |
else if (yClass == Ratio.class) | |
y = (Ratio) y; | |
return (Number) add(x, y); | |
} | |
} | |
Addition.add(new Integer(..), new Integer(..)) // Is good | |
Addition.add(new Ratio(..), new Ratio(..)) // Is good | |
Addition.add(new Ratio(..), new Integer(..)) // Infinite recursion in the add(Number, Number) definition |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment