简体   繁体   English

Java方法调用是不明确的

[英]Java method call is ambiguous

there are 2 methods: 有两种方法:

public static TermsQueryBuilder termsQuery(String name, int... values) {
    return new TermsQueryBuilder(name, values);
}
public static TermsQueryBuilder termsQuery(String name, Object... values) {
    return new TermsQueryBuilder(name, values);
}

when i called 我打电话的时候

termsQuery("operatorType", 1);

it says that is ambiguous. 它说这是模棱两可的。

Please Help ... 请帮忙 ...

all the methods are in Elasticsearch,for example: 所有方法都在Elasticsearch中,例如:

public static TermsQueryBuilder termsQuery(String name, String... values) {
    return new TermsQueryBuilder(name, values);
}
public static TermsQueryBuilder termsQuery(String name, int... values) {
    return new TermsQueryBuilder(name, values);
}
public static TermsQueryBuilder termsQuery(String name, long... values) {
    return new TermsQueryBuilder(name, values);
}
...
public static TermsQueryBuilder termsQuery(String name, Object... values) {
    return new TermsQueryBuilder(name, values);
}

when I called termsQuery("operationType","1") , not ambiguous. 当我调用termsQuery("operationType","1") ,并不含糊。

when I called termsQuery("operationType",1) , ambiguous. 当我调用termsQuery("operationType",1) ,含糊不清。

when I called termsQuery("operationType",Arrays.asList(searchParam.getOperatorType()) , not ambiguous. 当我调用termsQuery("operationType",Arrays.asList(searchParam.getOperatorType()) ,并不含糊。

puzzled..... 困惑.....

Simply rename one or both of your methods and the error will go away. 只需重命名一个或两个方法,错误就会消失。

You cannot overload this method with the signatures your desire, because your call can be matched by both method definitions, and the types int and Object are not in a superclass-subclass relationship, so neither is more specific than the other. 您不能使用您希望的签名重载此方法,因为您的调用可以由两个方法定义匹配,并且intObject类型不在超类 - 子类关系中,因此两者都不比另一个更具体。

Consider 考虑

integerTermsQuery

and

objectTermsQuery

EDIT 编辑

As you point out in your comments, the version with the String... parameter does not conflict with Object... and arrays are covariant with String being a subclass of Object . 正如您在注释中指出的那样,带有String...参数的版本与Object...不冲突Object...并且数组与StringObject的子类是协变的。

It's ambiguous because 1 can be automatically converted to Integer , which is accepted by the Object signature. 这是不明确的,因为1可以自动转换为Integer ,这是Object签名所接受的。 It can also be directly accepted by the int signature. 它也可以由int签名直接接受。

You should make these two methods unambiguous, by either renaming the methods, or not having such a generic argument list ( Object... ) 您应该通过重命名方法或不具有这样的通用参数列表( Object... )来明确这两种方法Object...

All constructors using varargs <any primitive>... are not callable directly using comma separated list of primitives if Object... exists because for all of those Object... is ambiguous. 如果Object...存在,那么使用varargs <any primitive>...所有构造函数都不能使用逗号分隔的基元列表直接调用,因为对于所有这些Object...都是不明确的。 Compiler error message clearly states that. 编译器错误消息明确指出。

We could call them using arrays: 我们可以使用数组调用它们:

termsQuery("operatorType", new int[]{1, 2, 3}) . termsQuery("operatorType", new int[]{1, 2, 3})

TermsQueryBuilder termsQuery(String name, Object... values) and TermsQueryBuilder termsQuery(String name, String... values) are not ambiguous because while String is also an Object it is a more special object than just Object . TermsQueryBuilder termsQuery(String name, Object... values)TermsQueryBuilder termsQuery(String name, String... values)因为String也是一个Object它是一个比Object更特殊的对象。

The following code works and should show what happens exactly: 以下代码有效,应该显示正是发生的情况:

public class TestAmbiguous {

 public static TermsQueryBuilder termsQuery(String name, int... values) {
    return new TermsQueryBuilder(name, values);
 }
 public static TermsQueryBuilder termsQuery(String name, double... values) {
    return new TermsQueryBuilder(name, values);
 }
 public static TermsQueryBuilder termsQuery(String name, Object... values) {
    return new TermsQueryBuilder(name, values);
 }
 public static TermsQueryBuilder termsQuery(String name, String... values) {
    return new TermsQueryBuilder(name, values);
 }

 public static void main(String[] args) {
  TermsQueryBuilder builder;
  builder = termsQuery("operatorType", 1, 1.0, 1.0f, "test", new Integer(1), new Double(1), new Float(1), new String("test"));
  builder = termsQuery("operatorType", "test", new String("test"));
  builder = termsQuery("operatorType", new int[]{1, 2, 3});
  builder = termsQuery("operatorType", new double[]{1, 2, 3});
  builder = termsQuery("operatorType", new float[]{1, 2, 3});
  builder = termsQuery("operatorType", (Object[]) new Float[]{1f, 2f, 3f});
 }
}

class TermsQueryBuilder {
 TermsQueryBuilder(String name, int... values) {
  System.out.println("_____________________________________");
  System.out.println("primitive int");
  for (int i = 0 ; i < values.length; i++) {
   System.out.println(values[i]);
  }
 }
 TermsQueryBuilder(String name, double... values) {
  System.out.println("_____________________________________");
  System.out.println("primitive double");
  for (int i = 0 ; i < values.length; i++) {
   System.out.println(values[i]);
  }
 }
 TermsQueryBuilder(String name, Object... values) {
  System.out.println("_____________________________________");
  System.out.println("Any objects");
  for (int i = 0 ; i < values.length; i++) {
   System.out.println(values[i].getClass());
   System.out.println(values[i]);
  }
 }
 TermsQueryBuilder(String name, String... values) {
  System.out.println("_____________________________________");
  System.out.println("String objects");
  for (int i = 0 ; i < values.length; i++) {
   System.out.println(values[i].getClass());
   System.out.println(values[i]);
  }
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM