gst*_*low 1 java inheritance overloading
让我们研究以下代码:
public class App {
public static class A {
public void doSmth3(long a) {
System.out.println("This is doSmth3() in A...");
}
}
public static class B extends A {
public void doSmth3(int a) {
System.out.println("This is doSmth3() in B...");
}
}
public static void test(A a) {
a.doSmth3(1);
}
public static void main(String[] args) {
test(new B());
new B().doSmth3(3);
}
}
Run Code Online (Sandbox Code Playgroud)
输出继电器:
This is doSmth3() in A...
This is doSmth3() in B...
Run Code Online (Sandbox Code Playgroud)
从我这边2线主要应该提供相同的结果但结果是不同的.
我的意见This is doSmth3() in A...应该输出twise,因为它正在超载.
请解释输出
简单:当Java编译器看到对a.doSmth3(1)内部的调用时test(A),它只能将其编译为调用A#doSmth3(long),这是唯一可用的方法.请注意,B#doSmth3(int)是超负荷的A#doSmth3(long),而不是替代.
| 归档时间: |
|
| 查看次数: |
104 次 |
| 最近记录: |