请考虑以下代码:
public class A {
public static void main(String[] args) {
Runnable test1 = ((I)(new I() {}))::test; // compiles OK
Runnable test2 = ((new I() {}))::test; // won't compile
}
interface I {
private void test() {}
}
}
Run Code Online (Sandbox Code Playgroud)
我真的不明白......我明白这种test()方法是私密的.但是,如果我们将匿名类强制转换为其接口,会发生什么变化((I)(new I() {}))?更确切地说,我希望看到一个允许该技巧的特定JLS点.
PS我已将其报告为编译器的错误(ID:9052217).在我看来,Runnable test2 = ((new I() {}))::test;在这种特殊情况下应该编译得很好.
PPS到目前为止,根据我的报告创建了一个错误:https://bugs.openjdk.java.net/browse/JDK-8194998.它可能会被关闭为"不会修复"或者什么.