gpt4 book ai didi

java: 对 .. 的引用是不明确的,方法 method1 和 method2 in ... 都匹配

转载 作者:搜寻专家 更新时间:2023-10-31 20:20:27 44 4
gpt4 key购买 nike

<分区>

我遇到了同样的代码:

    public class Devk{
public static void tMeth(Integer... i){
System.out.print("A");
}
public static void tMeth(int... i){
System.out.print("B");
}
public static void main(String args[]){
tMeth(Integer.valueOf("7"));//compile error
tMeth(new int[2]); //returns B
tMeth(new Integer[2]); //returns A
}

}

调用后我看到了

java: reference to tMeth is ambiguous, both method tMeth(java.lang.Integer...) in GenericsTest.Test1 and method tMeth(int...) in GenericsTest.Test1 match

方法 Integer.valueOf("7") 返回整数包装器。我希望在控制台中看到 A

谁能解释这种行为并为此提供一般规则?

附言

public static void tMeth(Integer i){
System.out.print("A");
}
public static void tMeth(int i){
System.out.print("B");
}
public static void main(String args[]){
tMeth(1); //returns B
tMeth(new Integer(1)); //returns A
}

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