gpt4 book ai didi

java - Java 8 中的方法不明确,为什么?

转载 作者:太空狗 更新时间:2023-10-29 22:46:16 24 4
gpt4 key购买 nike

<分区>

public static void main(String... args){
then(bar()); // Compilation Error
}

public static <E extends Exception> E bar() {
return null;
}

public static void then(Throwable actual) { }

public static void then(CharSequence actual) { }

编译结果(来自命令行javac Ambiguous.java)

Ambiguous.java:4: error: reference to then is ambiguous
then(bar());
^
both method then(Throwable) in Ambiguous and method then(CharSequence) in Ambiguous match
1 error

为什么这个方法有歧义?此代码在 Java 7 下编译成功!

将方法栏更改为:

public static <E extends Float> E bar() {
return null;
}

编译没有任何问题,但在 IntelliJ Idea 中报告为错误(无法解析方法 then(java.lang.FLoat))。

此代码在 Java 7 下失败 - javac -source 1.7 Ambiguous.java:

Ambiguous.java:4: error: no suitable method found for then(Float)
then(bar());
^
method Ambiguous.then(Throwable) is not applicable
(argument mismatch; Float cannot be converted to Throwable)
method Ambiguous.then(CharSequence) is not applicable
(argument mismatch; Float cannot be converted to CharSequence)
1 error

Java 版本

java version "1.8.0_40"
Java(TM) SE Runtime Environment (build 1.8.0_40-b25)
Java HotSpot(TM) 64-Bit Server VM (build 25.40-b25, mixed mode)

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com