简体   繁体   English

Java如何选择使用哪个构造函数?

[英]How does Java choose which constructor to use?

I am not able to understand the output of the following program. 我无法理解以下程序的输出。

public class Confusing {

    private Confusing(Object o) {
        System.out.println("Object");
    }

    private Confusing(double[] dArray) {
        System.out.println("double array");
    }

    public static void main(String[] args) {
        new Confusing(null);
    }
}

The correct output is "double array". 正确的输出是“双数组”。 WHy was this constructor chosen as more specific than the other when both can accept null? 当两个都可以接受null时,这个构造函数被选择为比另一个更具体吗?

尽管两个构造函数都可以接受null ,但double[] 继承java.lang.Object ,因此更具体。

The challenge of compiling dynamically typed languages is how to implement a runtime system that can choose the most appropriate implementation of a method or function — after the program has been compiled. 编译动态类型语言的挑战是如何实现一个运行时系统,该系统可以选择最合适的方法或函数实现 - 在编译程序之后。 Treating all variables as objects of Object type would not work efficiently. 将所有变量视为对象类型的对象将无法有效工作。

Hence, choosing the specific one over Object . 因此,选择特定的Object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM