变量参数函数歧义

发布于 2024-09-24 21:59:43 字数 357 浏览 4 评论 0原文

   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 fun(int ... a)
   {
     return 0;  
   }

输出: 1

问题: 在上述情况下,为什么函数 fun 选择第一个函数而不是第二个函数。既然无法确定用户实际想要调用哪个函数,那么选择的依据是什么?

   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 fun(int ... a)
   {
     return 0;  
   }

Output:
1

Question:
In the above case why does the function fun select the 1st function and not the second.On what basis is the selection done since there is no way to determine which fun the user actually wanted to call ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

逆流 2024-10-01 21:59:43

基本上有一个特定呼叫的偏好。除此之外,这意味着可以针对少量参数进行优化,避免在执行时毫无意义地创建数组。

JLS 并没有说得很清楚,但它位于 第 15.12.2.5 节,该部分讨论了如果某些条件成立,则固定数量方法比其他方法更具体 - 在本例中确实如此。基本上它更具体,因为有更多的调用对 varargs 方法有效,就像参数数量相同但参数类型本身更通用一样。

Basically there's a preference for a specific call. Aside from anything else, this means it's possible to optimize for small numbers of arguments, avoiding creating an array pointlessly at execution time.

The JLS doesn't make this very clear, but it's in section 15.12.2.5, the part that talks about a fixed arity method being more specific than another method if certain conditions hold - and they do in this case. Basically it's more specific because there are more calls which would be valid for the varargs method, just as if there were the same number of parameters but the parameter types themselves were more general.

葮薆情 2024-10-01 21:59:43

当遇到此类歧义时,编译器总是选择精确的方法。在您的场景中,具有三个参数的 func 比变量参数之一更精确,因此被调用。

编辑:根据双向飞碟的评论进行编辑。

Compiler always selects the precise method when these kind of ambiguities are encountered. In your scenario func with three arguments is more precise than the variable arguments one so that is called.

EDIT: Edited as per skeet's comment.

冷默言语 2024-10-01 21:59:43

如果测试此表达式:

    System.out.println(fun(2,3,4));
    System.out.println(fun(2,3));
    System.out.println(fun(2,3,4,7));

输出

1
0
0

为 Java 编译器首先检查其声明与调用的确切参数匹配的方法,否则,它会搜索匹配的替代方法。

If you test this expressions:

    System.out.println(fun(2,3,4));
    System.out.println(fun(2,3));
    System.out.println(fun(2,3,4,7));

The output is

1
0
0

The Java compiler first checks for a method whose declaration matchs the exact parameters of the invocation, and otherwise, it searches for the alternative method matching.

上课铃就是安魂曲 2024-10-01 21:59:43

此行为允许解析两个重载的变量参数调用:

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

调用 addStates() 会产生编译歧义问题。添加第三个方法:

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

这允许选择更具体的第三个 addStates() 来解决不确定要调用哪个变量类型参数方法的歧义问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文