Oracle编译器抛出错误:
X.java:2:错误:不兼容类型:INT#1不是功能界面 Object o =(I& J)() - > ; {}; ^ 在接口INT#1 中找到的多个非重载抽象方法其中INT#1是交集类型: INT#1扩展Object,I,J 1错误Eclipse编译器编译正常。
上述示例的修改形式:
public class X { Object o =(I& J)() - > {}; } 接口I { public void foo(); } 接口J { public void foo(); }Eclipse编译器抛出一个错误。 Oracle编译器接受它。
我认为Oracle编译器是正确的。
考虑测试用例:
interface I { default void foo(){System.out.println(foo I \\\); } default void bar(){System.out.println(bar I \\\); } } interface J extends I { default void foo(){System.out.println(foo J \\\); } } public class Y { public static void main(String argv [])throws Exception { J j = new J(){}; ((I& J)j).foo(); ((I& J)j).bar(); }}
Oracle和Eclipse编译器的输出是:
foo J bar I根据输出,我可以得出结论,Oracle看起来正确。
让我知道你们如何解释它。
谢谢
解决方案在第一个例子中, I& J 不是功能界面(只有一个抽象非对象方法)。所以javac是错误的。
在第二种情况下, I& J 是一个功能界面,所以javac再次是正确的。
听起来像Eclipse编译器中的两个错误。
public class X { Object o = (I & J) () -> {}; } interface I { public void foo(); } interface J { public void foo(); public void bar(); }Oracle compiler throws a error:
X.java:2: error: incompatible types: INT#1 is not a functional interface Object o = (I & J) () -> {}; ^ multiple non-overriding abstract methods found in interface INT#1 where INT#1 is an intersection type: INT#1 extends Object,I,J 1 errorEclipse compiler compiles fine.
Which implementation looks correct ?
A modified form of the above example:
public class X { Object o = (I & J) () -> {}; } interface I { public void foo(); } interface J { public void foo(); }Eclipse compiler throws a error. Oracle compiler accepts it.
I think Oracle compiler is correct.
Consider the test case:
interface I { default void foo() { System.out.println("foo I \n"); } default void bar() { System.out.println("bar I \n"); } } interface J extends I { default void foo() { System.out.println("foo J \n"); } } public class Y { public static void main(String argv[]) throws Exception { J j = new J() { }; ((I & J) j).foo(); ((I & J) j).bar(); }}
The output with Oracle and Eclipse Compiler is:
foo J bar IBased on the output I can conclude that Oracle looks correct.
Let me know how you guys interpret it.
Thanks
解决方案In the first example, I&J is not a functional interface (interface with exactly one abstract non-Object method). So javac is correct to give an error.
In the second case, I&J is a functional interface, so javac is again correct.
Sounds like two bugs in the Eclipse compiler.
更多推荐
编译错误:Lambda目标类型交点类型
发布评论