Variable argument function ambiguity

前端 未结 4 1152
鱼传尺愫
鱼传尺愫 2021-01-12 12:58
   public static void main(String[] args) {
       System.out.println(fun(2,3,4));
     }
   static int fun(int a,int b,int c)
   {
     return 1;
   }
   static int         


        
4条回答
  •  被撕碎了的回忆
    2021-01-12 13:11

    This behaviour allows the resolution of two overloaded variable argument calls:

    addStates(String... states)
    addStates(Enum... states)
    

    Calling addStates() gives a compile ambiguity problem. Adding in a third method:

    addStates(){
       addStates(new String[0]);
    }
    

    This allows selection selecting the more specific third addStates() to resolve the ambiguity problem of being unsure which of the variable type parameter methods to call.

提交回复
热议问题