为什么它(显然)有所作为是否我通过null直接作为参数,或者传递一个Object,我分配的价值 null?
Object testVal = null;
test.foo(testVal); // dispatched to foo(Object)
// test.foo(null); // compilation problem -> "The method foo(String) is ambiguous"
public void foo(String arg) { // More-specific
System.out.println("foo(String)");
}
public void foo(Object arg) { // Generic
System.out.println("foo(Object)");
}
Run Code Online (Sandbox Code Playgroud)
换句话说,为什么第二次调用(已注释掉)foo(...)未调度到foo(Object)?
更新:我使用Java 1.6.我可以毫无问题地编译Hemal的代码,但我仍然无法编译.我看到的唯一区别是,Hemal的方法是静态的,而我的方法则不是.但我真的不明白为什么这应该有所作为......?
更新2:解决了.我的班级中有另一个方法foo(Runnable),因此调度员无法明确地选择最具体的方法.(请参阅我在Hemal的第二个答案中的评论.)感谢大家的帮助.