Java 8:对[method]的引用是不明确的 [英] Java 8: Reference to [method] is ambiguous

查看:2122
本文介绍了Java 8:对[method]的引用是不明确的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人明白为什么下面的代码将在Java 7及以下版本中编译良好,但是在Java 8中失败。

Does anybody understand why the following code will compile fine in Java 7 and below, but fails with Java 8.

public static void main(String[] args) throws Exception {
    put(get("hello"));
}

public static <R> R get(String d) {
    return (R)d;
}

public static void put(Object o) {
    System.err.println("Object " + o);
}

public static void put(CharSequence c) {
    System.err.println("CharSequence " + c);
}

public static void put(char[] c) {
    System.err.println("char[] " + c);
}

get方法具有通用返回类型。在JDK 7及以下版本中,这将编译精细,并选择带有Object参数的put方法。在JDK 8中,这不能被编译,指示put方法是不明确的。

The get method has a generic return type. In JDK 7 and below this compiles fine and the put method with the Object parameter is chosen. In JDK 8 this cannot be compiled, indicating the put method is ambiguous.

显然,JDK 8跳过了Object-parameter方法并找到最后两个子Object-参数方法和抱怨(即如果你添加另一个put方法与其他参数类型,编译器将切换和抱怨关于新的最后两个方法)

Apparently JDK 8 is skipping over the Object-parameter method and finding the last two sub-Object-parameter methods and complaining about them (i.e. if you add another put method with some other parameter type, the compiler will switch and complain about the new last two methods)

这似乎

推荐答案

您的问题是广义目标类型推断的副作用, Java 8中的改进。

Your problem is a side-effect of Generalized Target-type Inference, an improvement in Java 8.

让我们以你的示例方法, p>

Let's take your example method,

public static <R> R get(String d) {
    return (R)d;
}



现在,在上面的方法中,通用参数 R 无法由编译器解析,因为没有 R 的参数。

Now, in the method above, the generic parameter R cannot be resolved by the compiler because there's no parameter with R.

,他们引入了一个名为 Target-type Inference 的概念,它允许根据赋值参数推断该参数。

So, they introduced a concept called Target-type Inference, which allows the parameter to be inferred based on the assignment parameter.

所以,如果你这样做,

 String str = get("something"); // R is inferred as String here
 Number num = get("something"); // R is inferred as Number here

这在Java 7中效果很好。不会,

This works well in Java 7. But the following does not,

put(get("something");
static void Put(String str) {} //put method

因为类型推理仅用于直接赋值。

Because type inference worked only for direct assignments.

如果没有直接赋值,那么泛型类型推断为 Object

If there's no direct assignment, then the generic type was inferred as Object.

所以,当你用Java 7编译代码时,你的 put(Object)方法被调用没有任何问题。

So, when you compiled the code with Java 7, your put(Object) method was called without any problems.

他们改进了类型推断 strong>方法调用和链接方法调用

They improved the type inference to infer the type from method calls and chained method calls

更多详细信息此处此处

现在,你可以直接调用 put(get(something)),泛型类型将根据 put()方法参数推断

So now, you can directly call put(get("something")) and the generic type will be inferred based on the parameter of the put() method.

但你知道,方法, put(Charsequence) put(char [])匹配的参数。

But as you know, the methods, put(Charsequence) and put(char[]) match the arguments. So there's the ambiguity.

只要告诉编译器你想要什么,

Just tell the compiler exactly what you want,

put(TestClass.<CharSequence>get("hello")); // This will call the put(CharSequence) method.

这篇关于Java 8:对[method]的引用是不明确的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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