代码之家  ›  专栏  ›  技术社区  ›  Yang Meyer

带有空参数的Java方法分派

  •  12
  • Yang Meyer  · 技术社区  · 17 年前

    为什么我通过考试(显然)会有不同 null 直接作为参数,或传递 Object 价值 无效的

    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)");
    }
    

    foo(...) 未派往 foo(Object) ?

    更新: 我使用Java1.6。我可以毫无问题地编译Hemal的代码,但我的代码仍然无法编译。我所看到的唯一区别是Hemal的方法是静态的,而我的方法不是静态的。但我真的不明白为什么这会有什么不同。。。?

    更新2:

    4 回复  |  直到 17 年前
        1
  •  24
  •   Miserable Variable    17 年前

    您使用的是哪个版本的Java?使用1.6.011编译并运行代码(粘贴在下面)。

    foo(testVal) foo(Object) .

    原因是什么 foo(null) foo(String) null nulltype ,它是所有类型的子类型。那么这个 空型 延伸 String ,扩展到 Object .

    当你打电话的时候 foo(空) 一串 那就更具体了 对象 这就是被调用的方法。

    foo(Integer) 然后您将得到一个不明确的重载错误。

    class NullType {
    
      public static final void main(final String[] args) {
        foo();
      }
    
      static void foo()
      {
        Object testVal = null;
        foo(testVal);    // dispatched to foo(Object)
        foo(null);    // compilation problem -> "The method foo(String) is ambiguous"   
      }
    
      public static void foo(String arg) { // More-specific
        System.out.println("foo(String)");
      }
    
      public static void foo(Object arg) { // Generic
        System.out.println("foo(Object)");
      }
    
    }
    
        2
  •  2
  •   L. Cornelius Dol    17 年前

        3
  •  1
  •   pgras    17 年前

    有人试过这个例子吗???

    如果添加一个新方法,比如foo(Integer),编译器将无法选择最具体的适用方法,并显示错误。

    -帕特里克

        4
  •  1
  •   Miserable Variable    17 年前

    @杨,我也能够编译和运行以下内容。你能发布一个完整的代码,用一行注释进行编译,这样如果我取消注释那一行,它就不会编译了吗?

    class NullType {
    
      public static final void main(final String[] args) {
        foo();
        new Test().bar(new Test());
      }
    
      static void foo()
      {
        Object testVal = null;
        foo(testVal);    // dispatched to foo(Object)
        // foo(null);    // compilation problem -> "The method foo(String) is ambiguous"   
      }
    
      public static void foo(String arg) { // More-specific
        System.out.println("foo(String)");
      }
    
      public static void foo(Integer arg) { // More-specific
        System.out.println("foo(Integer)");
      }
    
      public static void foo(Object arg) { // Generic
        System.out.println("foo(Object)");
      }
    
    
    }
    
    
    class Test
    {
      void bar(Test test)
      {
        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)");
      }
    }