为什么f(Double x)比f(double ... x)更好匹配? [英] Why is f(Double x) a better match than f(double... x)?
本文介绍了为什么f(Double x)比f(double ... x)更好匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
今天我正在攻读Java考试,我遇到了这个问题:
Today I was studying for an incoming Java exam and I ran into this question:
让
A
是一个定义如下的类:
Let
A
be a class defined as follows:
class A {
public void f(Double x) { System.out.println("A.f(Double)"); }
public void f(double... x) { System.out.println("A.f(double...)"); }
}
指令产生的输出是什么 A a =新A(); af(1.0);
?
What is the output produced by the instruction A a = new A(); a.f(1.0);
?
答案似乎是 Af(Double )
但我不明白为什么。有人可以给我一个正确的解释吗?
The answer seems to be A.f(Double)
but I can't understand why. Could someone give me a proper explanation?
推荐答案
重载决议总是倾向于一个带有明确数量的参数的函数变量参数列表,即使这意味着 1.0
是自动装箱的。
Overload resolution always favours a function with an explicit number of arguments over a function with a variable argument list, even if that means that 1.0
is auto-boxed.
稍微详细一点,根据 JLS 15.12.2 :
- 类型加宽
- 自动装箱
- 可变参数
这篇关于为什么f(Double x)比f(double ... x)更好匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文