Created
June 6, 2013 06:58
-
-
Save xfire/5719787 to your computer and use it in GitHub Desktop.
Joint union in type parameter variance.
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
class Test { | |
interface Foo { | |
int doFoo(); | |
} | |
interface Bar { | |
int doBar(); | |
} | |
// here we use the union type parameter: Foo & Bar | |
static class TypeUnion<T extends Foo & Bar> { | |
public int test(final T thingi) { | |
return thingi.doFoo() + thingi.doBar(); | |
} | |
} | |
static class Thingi implements Foo, Bar { | |
public int doFoo() { return 23; } | |
public int doBar() { return 42; } | |
} | |
public static void main(final String args[]) { | |
final TypeUnion<Thingi> tu = new TypeUnion<Thingi>(); | |
final Thingi thingi = new Thingi(); | |
System.out.println(" foo: " + thingi.doFoo()); | |
System.out.println(" bar: " + thingi.doBar()); | |
System.out.println("foo + bar: " + tu.test(thingi)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
see http://stackoverflow.com/questions/15496/hidden-features-of-java/42686#42686