没见过你这么玩的,呵呵下面这段程序也许能更好地说明问题,其中参数分别代表父类和他的子类  public void method(Object o) {
  System.out.println("Object Version");
  }
  public void method( Child d) {
  System.out.println("Child Version");
  }
    public void method(Parent i) {
  System.out.println("Parent Version");
  }
  public static void main(String[] args) {
  Test test = new Test();
  Object o=new Object();
  test.method(null);
  }java函数调用的时候会自动从子类开始检测
如果程序如下,怎会出错
public class Test {

  public void method(Object o) {
  System.out.println("Object Version");
  }
  public void method(String s) {
  System.out.println("String Version");
  }
  public void method(Integer i){
                   System.out.println("Integer Version");         }
 
 
 
  public static void main(String[] args) {
  Test test = new Test();
  Object o=new Object();
  test.method(null);
  }

解决方案 »

  1.   

    谢谢了(怎么给你分呀)
    我想再问一下,为什么我的程序(就是问题里的)运行后世String Version 而不是Object Version.
    还有什么叫“java函数调用的时候会自动从子类开始检测“
     谢谢
      

  2.   

    问题中的代码,结果是 string version 为什么不是 object version
      

  3.   

    就是函数调用,参数使用了null,但返回的结果确实“String Version”,而不是“Object version”的原因以及为什么这么用。
      

  4.   

    bluesmile979(笑着) 废话一大堆,可问题一点没说明白,真遗憾。
      

  5.   

    public void method(String s) {
      System.out.println("String Version");
      }
       public void method(Integer i){          System.out.println("Integer Version");
             }如果这两个函数同时存在于test类时,产生如下问题:
    reference to method is ambiguous; both method method(java.lang.String) and method method(java.lang.Integer) 也就是说null 在当做参数传递时,肯定不做为对象来处理,但又不能明确系统具体做为什么类型或者什么值来处理。
      

  6.   

    null的时候,程序会自动从子类开始检索,String 是属于Object的,所以就输出String Version了。