0x5*_*94E 5 java overloading compilation method-resolution-order
所以我有以下重载方法:
private static void foo(short... a)
{
System.out.println("Calling var-len");
}
private static void foo(int a, int b)
{
System.out.println("Calling int-int");
}
private static void foo(int a, double b) //(3)
{
System.out.println("Calling int-double");
}
private static void main (String[] args)
{
foo((short)2, (short)5); //This one outputs "Calling int-int"
}
Run Code Online (Sandbox Code Playgroud)
我知道变量arity方法在方法解析阶段具有最低优先级,所以在这种情况下如果我调用foo((short)2, (short)4);
我会得到"Calling int-int".
但是,如果我将方法(3)更改为foo(short a, double b)
,则选择变量arity方法!(Java 7).有人能解释一下吗?
根据规范:
\n\nhttp://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2
\n\n“但是,变量 arity 方法 (\xc2\xa78.4.1) 的声明可以更改为给定方法调用表达式选择的方法,因为变量 arity 方法在第一阶段被视为固定 arity 方法。”
\n