class A {
public void printFirst(int... va) throws IOException{
System.out.print("A");
}
public static void main(String args[]) {
try {
new B().printFirst(2);
} catch (Exception ex) {
}
}
}
class B extends A {
//@Override
public void printFirst(float... va) throws IOException{
System.out.print("B");
}
}
为什么,它显示引用调用不明确?
最佳答案
如果删除可变参数符号,它实际上会编译。文字 2
应被视为 int
,而不是 float
,因此我希望编译器会选择 A 中的 printFirst。
看起来这与编译器如何进行方法调用转换有关。 This SO question说它在规范中,但与这个问题相关的已接受答案的部分似乎是矛盾的(它说你不能将扩大转换(int到float)与可变参数结合起来,但后来它说这是可以的)。讨论了类似的问题in this question接受的答案得出的结论是,这种情况实际上是未指定的(不幸的是,讨论的链接现在已损坏)。更糟糕的是,语言指南只是建议 avoiding this type of overloading .
关于java - java中调用模糊的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14115408/