代码之家  ›  专栏  ›  技术社区  ›  theimpatientcoder

该方法对于类型Error不明确

  •  9
  • theimpatientcoder  · 技术社区  · 9 年前

    我试图理解JAVA中的重载是如何工作的,并试图掌握在JAVA中加宽、自动装箱和变参数的情况下应用的各种重载规则。我无法理解以下场景中发生的情况:

     package package1;
    
     public class JustAClass {
         public static void add(int a, long b) {
             System.out.println("all primitives");
         }
    
       //public static void add(Integer a, long b) {
       //     System.out.println("Wraper int, primitive long");
       //}
    
         public static void add(int a, Long b) {
             System.out.println("Primitive int, Wrapper long");
         }
    
         public static void add(Integer a, Long b){
             System.out.println("All wrapper");
         }
    
         public static void main(String[] args) {
            int a = 10;
            Integer b = 10;
            long c = 9;
            Long d = 9l;
    
            add(a,c);
            add(a,d);
            add(b,c);
            add(b,d);
     }
    
    }
    

    此时,在第三次调用 add 方法说明 The method is ambiguous for the type Error . 为什么会这样?确定哪种方法调用有效的规则是什么?下面的情况到底发生了什么? 我感觉到了 fourth 重载的add方法应该有效。请帮助我理解这背后的概念。

    1 回复  |  直到 9 年前