public class OverloadingTest extends Format{
public int add(String s1){
System.out.println("With String");
return 1;
}
public int add(Object a){
System.out.println("With Object");
return 1;
}
public static void main(String[] args) {
OverloadingTest overloadingTest = new OverloadingTest();
overloadingTest.add(null);
}
}
为什么程序With String ?
为什么这是程序的输出?
我曾尝试阅读JLS的第6版的输出,但我还是没能找到答案。
我可以猜测的唯一原因是选择了继承层次结构中最接近的匹配项。
所以在这种情况下,它将采取字符串作为对象是它的超类。
你回答了你自己的问题:) – 2013-04-10 05:28:43
的subber子类总是选择.... – Thihara 2013-04-10 05:29:41
@Deepak巴拉哦,太好了。但是,你有任何可以指出的参考或文件吗? – Sam 2013-04-10 05:30:18