Created
July 25, 2012 14:37
-
-
Save sndyuk/3176499 to your computer and use it in GitHub Desktop.
Java8 virtual extension method 色々
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
class VirtualExtensionMethod { | |
public static void main(String[] args) { | |
F fa = new FA(); | |
fa.f1("fa"); | |
F fb = new FB(); | |
fb.f2("fb"); | |
} | |
// error // public static <T extends Fx1> T makeFx(Class<T> clazz) { return (T)( (msg) -> { System.out.println(msg); }); } | |
//public static <T extends Fx1> T makeFx(Class<T> clazz) { return null; } | |
public static Fx1 makeFx1() { return (msg) -> {System.out.println(msg);}; } | |
// error // public static Fx2 makeFx2() { return (msg) -> {System.out.println(msg);}; } | |
public static Fx3 makeFx3() { return (msg) -> {System.out.println(msg);}; } | |
// error // public static F makeF() { return (msg) -> { System.out.println(msg); }; } | |
} | |
interface Fx1 { | |
void f(String msg); | |
} | |
interface Fx2 { | |
void f(String msg) default {}; | |
} | |
interface Fx3 { | |
void fx(String msg); | |
void f(String msg) default {}; | |
} | |
interface F { | |
F f = new FB(); | |
void f1(String msg); | |
void f2(String msg); | |
} | |
interface F1 extends F { | |
void f1(String msg) default { | |
f.f2(msg); | |
System.out.println("F1.f1 default " + msg); | |
} | |
void f2(String msg) default {} | |
} | |
interface F2 extends F { | |
void f2(String msg) default { | |
System.out.println("F2.f2 default " + msg); | |
} | |
} | |
class FA implements F1 { | |
public void f2(String msg) {System.out.println("FA.f2 " + msg);} | |
} | |
class FB implements F2 { | |
public void f1(String msg) {System.out.println("FB.f1 " + msg);} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment