作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
以下是Java中与构造函数重载相关的代码。我们来看看。
package temp;
final public class Main
{
private Main(Object o)
{
System.out.println("Object");
}
private Main(double[] da)
{
System.out.println("double array");
}
public static void main(String[] args)throws Exception
{
Main main = new Main(null);
}
}
在上面的代码中,构造函数被重载,其中一个具有 Object 类型的形参,另一个具有 double(数组)类型的形参。
Main main = new Main(null);
上面的语句调用了一个构造函数,该语句使用 null 值作为实际参数,程序在控制台上显示输出 double array .在这种情况下,编译器如何在运行时动态解析特定的构造函数(或方法,如果是这种情况)?
最佳答案
它在编译时解析为double[]
,因为它是most specific成员决定:
If more than one member method is both accessible and applicable to a method invocation, [...] The Java programming language uses the rule that the most specific method is chosen.
关于java - 令人困惑的Java方法重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8131203/
很抱歉新手的问题,但是: 我最近才发现“=”运算符不只是处理对象/等等。值(value),也是引用。这很酷,但我认为这对变量来说是不一样的,它不会在存储整数或 float 的变量之间创建引用。后来我觉
我是一名优秀的程序员,十分优秀!