以下程序的输出是1,3,3可以有人解释一下吗?它会将10.25视为方法参数的对象吗?
public class Test {
void methodOfTest(int i) {
System.out.println(1);
}
void methodOfTest(Integer I) {
System.out.println(2);
}
void methodOfTest(Object o) {
System.out.println(3);
}
public static void main(String[] args) {
Test t = new Test();
t.methodOfTest(10);
t.methodOfTest(10.25);
t.methodOfTest(new Double("25.25"));
}
}
Run Code Online (Sandbox Code Playgroud)
t.methodOfTest(10);
Run Code Online (Sandbox Code Playgroud)
10被解释为int文字,因此methodOfTest(int i)被称为
t.methodOfTest(10.25);
Run Code Online (Sandbox Code Playgroud)
没有方法,需要一个double,所以唯一10.25适合的方法是methodOfTest(Object o)
t.methodOfTest(new Double("25.25"));
Run Code Online (Sandbox Code Playgroud)
这里我们有一个Double对象,但是再一次,没有找到一个带有a Double的方法,所以唯一的方法是再次使用它methodOfTest(Object o).
因此你的输出是1,3,3.