Java如何选择要使用的构造函数? [英] How does Java choose which constructor to use?

查看:47
本文介绍了Java如何选择要使用的构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解以下程序的输出.

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);
    }
}

正确的输出是双数组".当两者都可以接受null时,为什么选择此构造函数比其他构造函数更具体?

The correct output is "double array". WHy was this constructor chosen as more specific than the other when both can accept null?

推荐答案

即使两个构造函数都可以接受 null double [] 继承来自 java.lang.Object ,因此更加具体.

Even though both constructors can accept null, double[] inherits from java.lang.Object, and is therefore more specific.

这篇关于Java如何选择要使用的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆