gpt4 book ai didi

java - 为什么构造函数调用在下面的例子中没有歧义?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:47:05 26 4
gpt4 key购买 nike

<分区>

class Test {

public Test(Object obj) {
System.out.println("Object");
}

public Test(String s) {
System.out.println("String");
}

public static void main(String[] args) {
new Test(null); //prints String. Why not Object?
}
}

如果我添加另一个参数类型为 Integer 的构造函数,或者就此而言任何其他类型,调用 new Test(null); 会导致编译错误 - 构造函数 Test(Object) 不明确

上面的例子为什么没有报错?在执行它时,调用带有参数 String 的构造函数。为什么不调用参数类型为 Object 的构造函数?这种歧义是如何解决的?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com