编程语言
首页 > 编程语言> > java – 重载var-args

java – 重载var-args

作者:互联网

谁能解释为什么第一种方法优于第二种?

我知道这个重载规则(除了首先编译器找到合适的args)

>扩大
>自动装箱
> var-args

码:

public class Proba{

    public static void show(Object ... args){
        System.out.println("Object ...");
    }

    public static void show(Integer[] ... args){
        System.out.println("Integer ...");
    }

    public static void main(String[] args) {
        Integer[] array = {3,2,5,1};
        show(array);        
    }
}

控制台:对象……

解决方法:

Java中的方法解析规则要求在尝试与这些功能匹配之前尝试匹配而不使用自动(非)装箱和变量arity.这可确保源代码与早于这些功能的语言版本兼容.

JLS(第15.12.2节)中描述了重载决策的规则:

The process of determining applicability begins by determining the potentially
applicable methods (§15.12.2.1).

The remainder of the process is split into three phases, to ensure compatibility with
versions of the Java programming language prior to Java SE 5.0. The phases are:

  1. The first phase (§15.12.2.2) performs overload resolution without permitting
    boxing or unboxing conversion, or the use of variable arity method invocation.
    If no applicable method is found during this phase then processing continues
    to the second phase.
    This guarantees that any calls that were valid in the Java programming language
    before Java SE 5.0 are not considered ambiguous as the result of the introduction of
    variable arity methods, implicit boxing and/or unboxing. However, the declaration of
    a variable arity method (§8.4.1) can change the method chosen for a given method
    method invocation expression, because a variable arity method is treated as a fixed
    arity method in the first phase. For example, declaring m(Object…) in a class which
    already declares m(Object) causes m(Object) to no longer be chosen for some
    invocation expressions (such as m(null)), as m(Object[]) is more specific.

  2. The second phase (§15.12.2.3) performs overload resolution while allowing
    boxing and unboxing, but still precludes the use of variable arity method
    invocation. If no applicable method is found during this phase then processing
    continues to the third phase.
    This ensures that a method is never chosen through variable arity method invocation
    if it is applicable through fixed arity method invocation.

  3. The third phase (§15.12.2.4) allows overloading to be combined with variable
    arity methods, boxing, and unboxing.

Deciding whether a method is applicable will, in the case of generic methods
(§8.4.4), require that type arguments be determined. Type arguments may be
passed explicitly or implicitly. If they are passed implicitly, they must be inferred
(§15.12.2.7) from the types of the argument expressions.

If several applicable methods have been identified during one of the three phases
of applicability testing, then the most specific one is chosen, as specified in section
§15.12.2.5.

在您的示例中,在步骤1中有两个候选项:具有Object []参数的方法和具有Integer [] []参数的方法.呼叫站点的参数类型是Integer [].由于Object []可以从Integer []中分配,但是Integer [] []不是,因此找到了一个适用的方法,并且重载决策在那里停止.在这种情况下,从未达到步骤2和3.

标签:java,overloading,variadic-functions
来源: https://codeday.me/bug/20190831/1775330.html