简体   繁体   English

为什么变量的未定义通用参数会丢弃其方法的返回类型的显式通用参数?

[英]Why undefined generic parameter of variable drops explicit generic parameter of it's method's return type?

public abstract class A<T> {
    public static void test(A i) { for (String s : i.get()) {} }
    public abstract Iterable<String> get();
}

Why for the code above I get: 为什么对于上面的代码我得到:

incompatible types
required: String
found:    Object

But if I will change argument of test method to A<Object> , it will compile OK? 但是如果我将test方法的参数更改为A<Object> ,它将编译好吗? Why undefined generic parameter of variable drops explicit generic parameter of it's method's return type? 为什么变量的未定义通用参数会丢弃其方法的返回类型的显式通用参数?

Why undefined generic parameter of variable drops explicit generic parameter of it's method's return type? 为什么变量的未定义通用参数会丢弃其方法的返回类型的显式通用参数?

A is a raw type. A原始类型。 That's a type with all generics dropped in the API dropped, even ones with fixed type arguments. 这种类型的所有泛型都被丢弃,即使具有固定类型参数的泛型也被删除。

See the JLS section 4.8 and the Raw Types section of the Java Generics FAQ for more information. 有关详细信息,请参阅Java Generics FAQJLS第4.8节Raw Types部分

In this case, if you want any A , you can use a wildcard: 在这种情况下,如果你想要的任何 A ,你可以使用通配符:

public static void test(A<?> i)

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

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