<legend id='KPTjp'><style id='KPTjp'><dir id='KPTjp'><q id='KPTjp'></q></dir></style></legend>

    <small id='KPTjp'></small><noframes id='KPTjp'>

      • <bdo id='KPTjp'></bdo><ul id='KPTjp'></ul>
      <i id='KPTjp'><tr id='KPTjp'><dt id='KPTjp'><q id='KPTjp'><span id='KPTjp'><b id='KPTjp'><form id='KPTjp'><ins id='KPTjp'></ins><ul id='KPTjp'></ul><sub id='KPTjp'></sub></form><legend id='KPTjp'></legend><bdo id='KPTjp'><pre id='KPTjp'><center id='KPTjp'></center></pre></bdo></b><th id='KPTjp'></th></span></q></dt></tr></i><div id='KPTjp'><tfoot id='KPTjp'></tfoot><dl id='KPTjp'><fieldset id='KPTjp'></fieldset></dl></div>
      <tfoot id='KPTjp'></tfoot>

      在 Java 7 中使用方法重载时,为什么自动装箱不推翻可变参数?

      Why doesn#39;t autoboxing overrule varargs when using method overloading in Java 7?(在 Java 7 中使用方法重载时,为什么自动装箱不推翻可变参数?)
    1. <i id='pcKrB'><tr id='pcKrB'><dt id='pcKrB'><q id='pcKrB'><span id='pcKrB'><b id='pcKrB'><form id='pcKrB'><ins id='pcKrB'></ins><ul id='pcKrB'></ul><sub id='pcKrB'></sub></form><legend id='pcKrB'></legend><bdo id='pcKrB'><pre id='pcKrB'><center id='pcKrB'></center></pre></bdo></b><th id='pcKrB'></th></span></q></dt></tr></i><div id='pcKrB'><tfoot id='pcKrB'></tfoot><dl id='pcKrB'><fieldset id='pcKrB'></fieldset></dl></div>
        <tbody id='pcKrB'></tbody>
      • <bdo id='pcKrB'></bdo><ul id='pcKrB'></ul>

        <tfoot id='pcKrB'></tfoot>

        <small id='pcKrB'></small><noframes id='pcKrB'>

          <legend id='pcKrB'><style id='pcKrB'><dir id='pcKrB'><q id='pcKrB'></q></dir></style></legend>

                本文介绍了在 Java 7 中使用方法重载时,为什么自动装箱不推翻可变参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我们的 Java 项目中有一个 LogManager 类,如下所示:

                We have a class LogManager in our Java project which looks like this:

                public class LogManager {
                
                    public void log(Level logLevel, Object... args) {
                        // do something
                    }
                
                    public void log(Level logLevel, int value, Object... args) {
                        // do something else
                    }
                }
                

                在Debianeveryting下使用OpenJDK 6编译项目时工作正常.当使用 OpenJDK 7 构建(使用 ant 完成)产生以下错误并且构建失败:

                When compiling the project with OpenJDK 6 under Debian everyting works fine. When using OpenJDK 7 the build (done with ant) produces the following errors and the build fails:

                [javac] /…/LogManager.java:123: error: reference to log is ambiguous,
                                      both method log(Level,Object...) in LogManager
                                      and method log(Level,int,Object...) in LogManager match
                [javac]       log(logLevel, 1, logMessage);
                [javac]       ^
                [javac] /…/SomeOtherClass.java:123: error: reference to log is ambiguous,
                                      both method log(Level,Object...) in LogManager
                                      and method log(Level,int,Object...) in LogManager match
                [javac]       logger.log(logLevel, 1, logMessage);
                [javac]             ^
                

                只要 1 没有自动装箱,方法调用应该是明确,因为 1 是一个 int 并且不能向上转换为 Object.所以为什么自动装箱不会在这里推翻可变参数吗?

                As long as the 1 is not autoboxed, the method call should be unambiguous as 1 is an int and cannot be upcast to Object. So why doesn't autoboxing overrule varargs here?

                Eclipse(使用来自 eclipse.org 的 tar.gz 安装)编译它没有无论是否安装了 OpenJDK 6.

                Eclipse (installed using the tar.gz from eclipse.org) compiles it no matter if OpenJDK 6 is installed or not.

                非常感谢您的帮助!

                编译器在这两种情况下都会获得选项 source="1.6"target="1.6".Eclipse 编译说明仅作为注释.

                The compiler gets the option source="1.6" and target="1.6" in both cases. The Eclipse compiling note is just meant as a comment.

                推荐答案

                估计和bug有关 #6886431,这似乎在 OpenJDK 7 中也已修复.

                I guess it's related to bug #6886431, which seems to be fixed in OpenJDK 7 as well.

                问题在于 JLS 15.12.2.5选择最具体的方法表示当前者的形式参数类型是后者的形式参数的子类型时,一种方法比另一种方法更具体.

                The problem is that JLS 15.12.2.5 Choosing the Most Specific Method says that one method is more specific than another one when types of formal parameters of the former are subtypes of formal parameters of the latter.

                由于 int 不是 Object 的子类型,因此您的方法都不是最具体的,因此您的调用不明确.

                Since int is not a subtype of Object, neither of your methods is the most specific, thus your invocation is ambiguous.

                但是,以下解决方法是可能的,因为 IntegerObject 的子类型:

                However, the following workaround is possible, because Integer is a subtype of Object:

                public void log(Level logLevel, Object... args) { ... }
                public void log(Level logLevel, Integer value, Object... args) { ... } 
                

                这篇关于在 Java 7 中使用方法重载时,为什么自动装箱不推翻可变参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Compiling C++ for the JVM(为 JVM 编译 C++)
                Compile to java bytecode (without using Java)(编译成java字节码(不使用Java))
                How to drive C#, C++ or Java compiler to compute 1+2+3+...+1000 at compile time?(如何在编译时驱动 C#、C++ 或 Java 编译器计算 1+2+3+...+1000?)
                Java ClassLoader: load same class twice(Java ClassLoader:两次加载相同的类)
                How to debug .class files in ECLIPSE?(如何在 ECLIPSE 中调试 .class 文件?)
                Java quot;The blank final field may not have been initializedquot; Anonymous Interface vs Lambda Expression(Java“可能尚未初始化空白的最终字段匿名接口与 Lambda 表达式)
                1. <i id='2TdTU'><tr id='2TdTU'><dt id='2TdTU'><q id='2TdTU'><span id='2TdTU'><b id='2TdTU'><form id='2TdTU'><ins id='2TdTU'></ins><ul id='2TdTU'></ul><sub id='2TdTU'></sub></form><legend id='2TdTU'></legend><bdo id='2TdTU'><pre id='2TdTU'><center id='2TdTU'></center></pre></bdo></b><th id='2TdTU'></th></span></q></dt></tr></i><div id='2TdTU'><tfoot id='2TdTU'></tfoot><dl id='2TdTU'><fieldset id='2TdTU'></fieldset></dl></div>

                  <tfoot id='2TdTU'></tfoot>
                    • <legend id='2TdTU'><style id='2TdTU'><dir id='2TdTU'><q id='2TdTU'></q></dir></style></legend>
                    • <small id='2TdTU'></small><noframes id='2TdTU'>

                          <tbody id='2TdTU'></tbody>

                          <bdo id='2TdTU'></bdo><ul id='2TdTU'></ul>