这只是一个练习,但我无法弄清楚其中的歧义:
private static void flipFlop(String str, int i, Integer iRef) {
System.out.println(str + "ciao");
}
private static void flipFlop(String str, int i, int j) {
System.out.println(str + "hello");
}
public static void main(String[] args) {
flipFlop("hello", new Integer(4), 2004);
}
它说:
The method flipFlop(String, int, Integer) is ambiguous for the type Test
我猜想第二个参数会被解包为 int,因此第二个 flipFlop
方法会成为选择。
最佳答案
如果您喜欢引人入胜的阅读,here is the relevant portion of the Java Language specification描述了方法是如何解析的。
但基本上你的第三个参数可以被解释为一个原始的或一个自动装箱的包装器,编译器无法弄清楚你想要什么。这两种方法都“最具体”以使用 JLS 术语。
关于java - 为什么两个带有签名(primitive, wrapper) 和(primitive, primitive) 的方法会导致方法调用(wrapper, primitive) 不明确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20923888/