【问题标题】:Where are the preferred argument conversions for overloaded methods documented (if at all)?重载方法的首选参数转换记录在哪里(如果有的话)?
【发布时间】:2010-02-18 07:55:32
【问题描述】:

换句话说,Java 编译器如何确定选择执行哪个重载方法的精确规则是什么?我花了很多时间在谷歌上搜索,但我认为我没有使用正确的搜索关键字。

public class C1  extends C2 {}
public class C2  extends C3 {}
public class C3 {}

public class Test {

    public static void main(String[] args) {
        C1 c1 = new C1();
        // What are the precise rules for determining 
        // which method below is called?
        method(c1, c1); 
    }

    static public void method(C3 test, C3 test2) {
        System.out.println("C3");
    }

    static public void method(C2 test, C3 test2) {
        System.out.println("C2");
    }

}

【问题讨论】:

    标签: java compiler-construction arguments overloading


    【解决方案1】:

    JLS 的相关部分是15.12.2 Compile-Time Step 2: Determine Method Signature。规则复杂且技术性强,但总的原则是选择最具体的参数类型的适用方法。

    【讨论】:

      【解决方案2】:

      我认为Java Language Spec, Conversions.中已经说明了这一点

      我可以从那里看到必须是扩大的参考转换,但我没有看到任何关于路径的评论。在我看来,它会搜索最短的匹配路径(在继承树中),因为这是最合乎逻辑的,但同样,我在规范中找不到。

      【讨论】:

        猜你喜欢
        • 2013-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-28
        • 1970-01-01
        相关资源
        最近更新 更多