具有独立于泛型类型的泛型方法的原始类型 [英] Raw types with generic methods independent of the generic type

查看:108
本文介绍了具有独立于泛型类型的泛型方法的原始类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对 chrert 的问题的后续跟踪。其他类型的集合获取器的泛型类。下面的代码包含一个泛型类 GenericClass< T> ;. 与一个返回类型 T 的方法和另一个返回类型集合< String> ,它显然独立于 T



现在,如果我实例化一个原始的 GenericClass (我永远不会这样做,所以这个问题更多的是一个理论问题,以帮助理解发生了什么),然后在增强for循环中调用该方法将不起作用,因为在使用原始类型时,所有泛型类型信息似乎都会丢失。但是,当在一个任务中调用相同的方法时,它会起作用(它会警告类型不安全,但会编译)。



在我看来,不工作,或两者都应该工作。我不明白为什么一个人工作,而另一个不是。您是否有任何提示,或者知道解释此行为的任何JLS部分?

  public class GenericClass< T> {

T doSomething(){
return null;
}

集合< String> getCollection(){
返回Collections.emptyList();


public static void main(String [] args){
GenericClass raw = new GenericClass();
//这将不会编译(下面的错误消息)
for(String str:raw.getCollection()){
//类型不匹配:无法从元素类型Object转换为字符串
}
//这只是一个警告:
//类型安全性:类型Collection的表达式需要未经检查的转换以符合Collection< String>
集合< String> coll = raw.getCollection();
for(String string:coll){
//正常工作
}
}
}






有一个相关的问题,与这里公认的答案一起,解释了很好的情况:为什么这个通用java代码不能编译?

解决方案

在第一种情况下, raw.getCollection()返回一个原始集合 JLS 14.14.2 指定为循环增强的类型检查:
$ b


如果Type FormalParameter生产)是一个引用类型,那么TargetType是Type;否则,TargetType是I,或Object如果我是raw的捕获转换的上界。 b $ b

(强调增加)

在第二种情况下,您明确地将一个原始类型分配给一个通用类型, 。


This a follow-up to chrert's question Generic classes with Collection getter of other types. If you can come up with a better title to my question, feel free to edit it:

Following code contains a generic class GenericClass<T> with a method of return type T and another method with return type Collection<String>, which is obviously independent of T.

Now, if I instantiate a raw GenericClass (which I would never do, so this question is more a theoretical question, to help understand what's going on) then calling that method in an enhanced for loop won't work, because all generic type information seem to get lost when using raw types. But then, when calling that same method in an assignment, it works (it warns about type unsafety, but it compiles).

In my point of view, either both shouldn't work, or both should work. I don't understand why one works, and the other doesn't. Do you have any hints, or know any parts of the JLS that explain this behavior?

public class GenericClass<T>  {

    T doSomething() {
        return null;
    }

    Collection<String> getCollection() {
        return Collections.emptyList();
    }

    public static void main(String[] args) {
        GenericClass raw = new GenericClass();
        // This will not compile (Error message below)
        for (String str : raw.getCollection()) {
            // Type mismatch: cannot convert from element type Object to String
        }
        // This is only a warning:
        // Type safety: The expression of type Collection needs unchecked conversion to conform to Collection<String>
        Collection<String> coll = raw.getCollection();
        for (String string : coll) {
            // works just fine
        }
    }
}


There is a related question, which, together with the accepted answer here, explains what's going on pretty good: Why won't this generic java code compile?

解决方案

In the first case, raw.getCollection() returns a raw Collection. JLS 14.14.2 specifies the type checking for the enhanced for loop:

If Type (in the FormalParameter production) is a reference type, then TargetType is Type; otherwise, TargetType is the upper bound of the capture conversion of the type argument of I, or Object if I is raw.

(emphasis added)

In the second case, you're explicitly assigning a raw type to a generic type, which is allowed with a warning like normal.

这篇关于具有独立于泛型类型的泛型方法的原始类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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