编译错误:Lambda目标类型交点类型

编程入门 行业动态 更新时间:2024-10-26 08:26:29
本文介绍了编译错误:Lambda目标类型交点类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 public class X { Object o =(I& J)() - > {}; } 接口I { public void foo(); } 接口J { public void foo(); public void bar(); }

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 error

Eclipse 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 I

Based 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目标类型交点类型

本文发布于:2023-11-25 17:31:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630686.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   交点   错误   目标   Lambda

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!