【发布时间】:2015-08-31 13:04:08
【问题描述】:
我对这个话题有点困惑,原因在这段代码中:
public class File
{
public static void main(String[] args)
{
numbers();
}
static void numbers(int...x)
{
System.out.println("Integers");
}
static void numbers(byte...x)
{
System.out.println("Byte");
}
static void numbers(short...x)
{
System.out.println("Short");
}
}
这段代码的输出是“字节”,原因是选择了最具体的类型,因为byte、short和int中最具体的类型是byte,这就是它的原因选择。
但是如果我将代码修改为-
public class File
{
public static void main(String[] args)
{
numbers(1, 4);
}
static void numbers(int...x)
{
System.out.println("Integers");
}
static void numbers(byte...x)
{
System.out.println("Byte");
}
static void numbers(short...x)
{
System.out.println("Short");
}
}
输出是“整数”,我不知道为什么?我知道在算术指令byte 和short 被隐式提升为int 的情况下,但在这里我们调用一个值在byte 和short 范围内的方法,那么该方法如何使用int 参数被调用?
另外,如果我用int 参数注释掉方法,那么代码会显示找不到合适方法的错误。为什么???
【问题讨论】:
标签: java int byte variadic-functions short