gpt4 book ai didi

java - 重载可变参数

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:48:38 25 4
gpt4 key购买 nike

谁能解释为什么第一种方法比第二种方法更可取?

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

  1. 拓宽
  2. 自动装箱
  3. 可变参数

代码:

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 中的方法解析规则要求在 auto-(un)boxing 和变量元数尝试匹配 em> 那些功能。这确保了源代码与早于这些功能的语言版本的兼容性。

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 - 重载可变参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19693650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com