1
根据我的理解,Java中的所有函数调用都是虚拟的,数字文字的类型为int。但为什么下面例子中的输出有所不同?Java虚拟函数调用
public class A {
public int f(long d) {
return 2;
}
}
public class B extends A {
public int f(int d) {
return 1;
}
}
public class M {
public static void main(String[] args) {
B b = new B();
A ab = b;
System.out.println(b.f(1));
System.out.println(ab.f(1));
}
}
'A'没有定义任何'f(int)'方法,所以显然在第二种情况下调用'f(long)'。代码中没有方法重写。 – Berger