public class Test1 { public static void main(String[] args) { Test1 test1 = new Test1(); test1.testMethod(null); } public void testMethod(String s){ System.out.println("Inside String Method"); } public void testMethod(Object o){ System.out.println("Inside Object Method"); } }
Inside String Method
解决方法
最重要的方法选择最具体的方法参数
在这种情况下,String是Object的子类.因此String变得比Object更具体.因此,打印了Inside String方法.
If more than one member method is both accessible and applicable to a method invocation,it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.
正如BMT和LastFreeNickName正确提示的那样,(Object)null将引起使用Object type方法的重载方法被调用.