java – 枚举之间的区别<?扩展ZipEntry>和枚举?

枚举之间有区别吗?扩展ZipEntry>和枚举< ZipEntry>?如果是这样,有什么区别?

解决方法

当您有其中之一时,您可以做什么,因为类型参数仅用于“输出”位置,所以没有实际的区别.另一方面,在您可以使用的其中一个方面有很大的区别.

假设你有枚举< JarEntry> – 你不能把它传给一个采取Enumeration< ZipEntry>作为其论据之一.您可以将其传递给一个采用枚举的方法扩展ZipEntry>虽然.

当您使用类型参数在输入和输出位置使用类型时,这更有意思 – List< T>是最明显的例子.以下是有关参数变化的方法的三个示例.在每种情况下,我们将尝试从列表中获取一个项目,并添加一个项目.

// Very strict - only a genuine List<T> will do
public void Foo(List<T> list)
{
    T element = list.get(0); // Valid
    list.add(element); // Valid
}

// Lax in one way: allows any List that's a List of a type
// derived from T.
public void Foo(List<? extends T> list)
{
    T element = list.get(0); // Valid
     // Invalid - this Could be a list of a different type.
     // We don't want to add an Object to a List<String>
    list.add(element);   
}

// Lax in the other way: allows any List that's a List of a type
// upwards in T's inheritance hierarchy
public void Foo(List<? super T> list)
{
    // Invalid - we Could be asking a List<Object> for a String.
    T element = list.get(0);
    // Valid (assuming we get the element from somewhere)
    // the list must accept a new element of type T
    list.add(element);
}

有关详细信息,请阅读:

> The Java language guide to generics
> The Java generics tutorial (PDF)
> The Java generics FAQ – 特别是the section on wildcards

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...