简体   繁体   English

使用带有Type参数的通配符需要额外的转换

[英]Using wild cards with Type parameter Needs extra casting

I have created two classes , Parent and child . 我创建了两个班,父母班和孩子班。 My Parent class is implementing Comparable interface as "class Parent implements Comparable" 我的父类正在实现Comparable接口,因为“父类实现了Comparable类”

I have also written a method which returns the maximum element of a list , here i have ? 我还写了一个方法,该方法返回列表的最大元素,这里我有? super T wild card to accept any Child class of Parent . 超级T通配符接受任何父母的子女类别。

The following code works fine on a list of Child Objects . 以下代码在子对象列表上正常工作。

public static <T extends Comparable<? super T>> T max(List <T> list){
    Iterator< T> itr = list.iterator();   // line xx
    T result = itr.next();
    T temp ;
    while(itr.hasNext()){
        temp = itr.next();
        if( temp.compareTo(result) > 0)
            result = temp ;
    }
    return result ;
}

But if i change the proto type of max method to accept any List of SubType of T by using < ? 但是如果我通过使用<?将max方法的原型类型更改为接受T的SubType的任何列表。 extends T > then the above code does not compile . 扩展T>则以上代码将无法编译。

Compliation Error is : cannot convert from Iterator to Iterator 编译错误为:无法从Iterator转换为Iterator

on changing line xx to " Iterator< T> itr = (Iterator) list.iterator();" 在将第xx行更改为“ Iterator <T> itr =(Iterator)list.iterator();” the code works with < ? 该代码适用于<? extends T > argument . 扩展T>参数。

public static <T extends Comparable<? super T>> T max(List <? extends T> list){
    Iterator< T> itr = (Iterator<T>) list.iterator(); // works fine
    Iterator< T> itr1 =  list.iterator();            // generates compile error .
    T result = itr.next();
    T temp ;
    while(itr.hasNext()){
        temp = itr.next();
        if( temp.compareTo(result) > 0)
            result = temp ;
    }
    return result ;
}

I am not getting it that why i compile time error is thrown . 我不明白为什么我会编译时间错误。

Complete Code follows as : 完整的代码如下:

package Generics;

import java.util.*;

public class WildCard {
public static void main(String[] args) {
    List<Child> cList = Arrays.asList(new Child("c1"),new Child("c3"),new Child("c2"));
    System.out.println(max(cList));
}

public static <T extends Comparable<? super T>> T max(List <? extends T> list){
    Iterator< T> itr = (Iterator<T>) list.iterator();
    T result = itr.next();
    T temp ;
    while(itr.hasNext()){
        temp = itr.next();
        if( temp.compareTo(result) > 0)
            result = temp ;
    }
    return result ;
}
}
class Parent implements Comparable<Parent>{
    private String val ;
    public Parent(String val){
        this.val = val ;
    }
    @Override
    public int compareTo(Parent o) {
        // TODO Auto-generated method stub
        return this.val.compareTo(o.val);
    }
    public String toString(){
        return val ;
    }
}
class Child extends Parent{

    public Child(String val) {
        super(val);
    }

}

My doubt is even when i was not using ? 我的疑问是什至我不使用时? extends T in function argument , i was able to accept the subtypes and i did not need any casting . 在函数参数中扩展了T,我能够接受子类型,而且我不需要任何转换。 But why on changing that syntax i need type casting ? 但是,为什么要更改该语法,我需要进行类型转换?

Since you have a List<? extends T> 由于您有List<? extends T> List<? extends T> , you should use an Iterator<? extends T> List<? extends T> ,您应该使用Iterator<? extends T> Iterator<? extends T> : Iterator<? extends T>

Iterator<? extends T> itr = list.iterator();

And Iterator is a producer of T anyway, so according to the PECS rule (Producer extends , Consumer super ), so it's natural that Iterator should with extends anyway. 而且Iterator无论如何都是T的生产者,所以根据PECS规则(生产者extends ,消费者super ),所以Iterator无论如何都要自然地extends

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

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