首页 / JAVA / java – 重载var-args
java – 重载var-args
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 重载var-args,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2969字,纯文字阅读大概需要5分钟。
内容图文
![java – 重载var-args](/upload/InfoBanner/zyjiaocheng/724/262eec8335c447279f4a08d273a5baae.jpg)
谁能解释为什么第一种方法优于第二种?
我知道这个重载规则(除了首先编译器找到合适的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:
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.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.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 – 重载var-args全部内容,希望文章能够帮你解决java – 重载var-args所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。