Phi*_* B. 4 java compiler-construction methods
我正在搞乱方法并且正在寻找,如果我使用他们想要的不同对象制作两个名为"hello"的方法并且将"null"传递给方法,那么将执行哪个方法:
public static void main(String... args) {
hello(null);
}
public static void hello(Window w) {
System.out.println("Hello");
}
public static void hello(Frame f) {
System.out.println("Bye");
}
Run Code Online (Sandbox Code Playgroud)
输出是每次"再见",但我仍然不明白背后的逻辑.经过与谷歌的短暂研究,没有任何解释,我决定在这里问这个问题.
我希望有人可以解释选择算法或给我一个解释的链接.
编译器更喜欢最专业的类型:
public class Parent {
public static void main(String... args) {
hello(null); // takes `Child`
}
public static void hello(Parent _) {
System.out.println("SuperClass");
}
public static void hello(Child _) {
System.out.println("SubClass");
}
}
class Child extends Parent {
}
Run Code Online (Sandbox Code Playgroud)
出于这个原因,请看看@Hayden在他的评论中已经提到过的这个帖子.
| 归档时间: |
|
| 查看次数: |
112 次 |
| 最近记录: |