变量参数构造函数_may_冲突,但编译 [英] Variable argument constructor _may_ conflict, but compiles

查看:180
本文介绍了变量参数构造函数_may_冲突,但编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个构造函数,编译只是罚款,但我希望Java可以抱怨歧义的可能性。

  Foo(int id,Bar bar,String name,String description){
}

public Foo(int id,Bar bar,String ... values){
} $

解决方案 / div>

Java允许这些方法存在,因为它有规则,如果两者都适用,将调用哪一个。具体来说,将使用变量arity方法(使用 ... ... >)。



JLS,Section 15.12.2 ,在确定选择哪种方法时说明以下内容:


第一阶段(§15.12.2.2)执行重载解析,而没有
允许装箱或拆箱转换,或使用变量arity
方法调用。如果在该阶段期间没有找到适用的方法
,则处理继续到第二阶段。



这保证在Java编程中有效的任何调用$ b在Java SE 5.0之前的$ b语言不被认为是不明确的结果
的引入变量arity方法,隐式装箱和/或
unboxing。然而,变量arity方法(§8.4.1)的声明
可以改变为给定的方法调用
表达式选择的方法,因为变量arity方法被视为固定的
第一阶段的arity方法。例如,在已经声明m(Object)的类中声明m(Object ...)
导致对于一些调用表达式(例如m(null))选择更长的m(Object) )
m(Object [])更具体。



第二阶段(§15.12.2.3)执行重载解析,
允许拳击和拆箱,但仍然排除使用变量
arity方法调用。如果在此
阶段期间没有找到适用的方法,则处理继续到第三阶段。



这确保不会通过变量arity



第三阶段(§15.12.2.4)允许重载


(强调我的)



示例代码:

  class Bar {} 

public class Foo {
public static void main(String [] args){
Foo main = new Foo(1,new Bar(),name,description);
Foo main2 = new Foo(2,new Bar(),name);
Foo main3 = new Foo(3,new Bar(),name,description,otherValues);
Foo main4 = new Foo(4,new Bar());
}

public Foo(int id,Bar bar,String name,String description){
System.out.println(name and description!
}

public Foo(int id,Bar bar,String ... values){
System.out.println(values!
}
}

这将输出:

 名称和说明! 
values!
values!
values!

...表示Java将选择固定的arity方法。

I have two constructors that compile just fine but I'd expect Java to complain about the possibility of ambiguity.

public Foo(int id, Bar bar, String name, String description){
}

public Foo(int id, Bar bar, String... values){
}

What gives?

解决方案

Java allows these methods to exist, because it has rules about which one will be called if both apply. Specifically, the fixed arity method (without ...) will be chosen over the variable arity method (with ...).

The JLS, Section 15.12.2, states the following when determining which method is chosen:

The first phase (§15.12.2.2) performs overload resolution without permitting boxing or unboxing conversion, or the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the second phase.

This guarantees that any calls that were valid in the Java programming language before Java SE 5.0 are not considered ambiguous as the result of the introduction of variable arity methods, implicit boxing and/or unboxing. However, the declaration of a variable arity method (§8.4.1) can change the method chosen for a given method method invocation expression, because a variable arity method is treated as a fixed arity method in the first phase. For example, declaring m(Object...) in a class which already declares m(Object) causes m(Object) to no longer be chosen for some invocation expressions (such as m(null)), as m(Object[]) is more specific.

The second phase (§15.12.2.3) performs overload resolution while allowing boxing and unboxing, but still precludes the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the third phase.

This ensures that a method is never chosen through variable arity method invocation if it is applicable through fixed arity method invocation.

The third phase (§15.12.2.4) allows overloading to be combined with variable arity methods, boxing, and unboxing.

(emphasis mine)

Example code:

class Bar{}

public class Foo{
   public static void main (String [] args){
      Foo main = new Foo(1, new Bar(), "name", "description");
      Foo main2 = new Foo(2, new Bar(), "name");
      Foo main3 = new Foo(3, new Bar(), "name", "description", "otherValues");
      Foo main4 = new Foo(4, new Bar());    
   }

   public Foo(int id, Bar bar, String name, String description) {
      System.out.println("name and description!");
   }

   public Foo(int id, Bar bar, String... values) {
      System.out.println("values!");
   }
}

This prints out:

name and description!
values!
values!
values!

... to show that Java will pick the fixed arity method if it can.

这篇关于变量参数构造函数_may_冲突,但编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆