如何解决此Java泛型通配符错误?

前端之家收集整理的这篇文章主要介绍了如何解决此Java泛型通配符错误? 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在此question中,TofuBeer在创建通用IterableEnumeration时遇到问题.

答案来自jcrossley3,指向该链接http://www.javaspecialists.eu/archive/Issue107.html几乎解决了问题.

我仍然没有一件事.正如埃里克森有效指出的那样,真正的问题是:

You cannot specify a wildcard when constructing a parameterized type

但是删除声明中的通配符也不起作用:

final IterableEnumeration<ZipEntry> iteratable 
                  = new IterableEnumeration<ZipEntry>(zipFile.entries());

导致以下错误

Main.java:19: cannot find symbol
symbol  : constructor IterableEnumeration(java.util.Enumeration<capture#469 of ? extends java.util.zip.ZipEntry>)
location: class IterableEnumeration<java.util.zip.ZipEntry>
        final IterableEnumeration<ZipEntry> iteratable = new IterableEnumeration<ZipEntry>(  zipFile.entries());
                                                         ^
1 error

但是JavaSpecialist中的示例可以工作:

  IterableEnumeration<String> ie =
              new IterableEnumeration<String>(sv.elements());

我可以发现的唯一区别是,在JavaSpecialists博客中,枚举来自一个Vector,其签名为:

public Enumeration<E> elements()

而失败的源于ZipFile,其签名为:

public Enumeration<? extends ZipEntry> entries()

最后,所有这些都被for-each构造和链接中建议的静态make方法吸收了

for(final ZipEntry entry : IterableEnumeration.make( zipFile.entries() ))  {
    if(!(entry.isDirectory())) {
        names.add(entry.getName());
    }
}

但!!该新闻简报的重点不是解决此问题,而是避免因为语法看起来很丑而需要指定泛型类型!

所以..我的问题是:

怎么了?

当参数是类型为<的Enumeration时,为什么不能创建IterableEnumeration实例?扩展SomeClass> ?以及为什么make for-each构造会吞没这个问题?!

为什么这样做:

for(final ZipEntry entry : IterableEnumeration.make( zipFile.entries() ))  {

但这行不通吗?

final IterableEnumeration<ZipEntry> iteratable
                     = IterableEnumeration.make( zipFile.entries() );

以下是TofuBeer原始代码的(略)修改版本:

import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.Vector;

public class Main {
    private ZipFile zipFile;

    public Set<String> entries() {

        final Vector<ZipEntry>    vector = new Vector<ZipEntry>();
        // why this works.
        //final IterableEnumeration<ZipEntry> iteratable = new IterableEnumeration<ZipEntry>( vector.elements() );

        // but this do not.
        //final IterableEnumeration<ZipEntry> iteratable = new IterableEnumeration<ZipEntry>( zipFile.entries() );

        // nor this 
        final IterableEnumeration<ZipEntry> iteratable = IterableEnumeration.make( zipFile.entries() );

        // And what's with the for-each that doesn't care about the type?    
        final Set<String>   names = new HashSet<String>();

        for(final ZipEntry entry : IterableEnumeration.make( zipFile.entries() ))  {
            if(!(entry.isDirectory())) {
                names.add(entry.getName());
            }
        }

        return (names);
    }
}

class IterableEnumeration<T> implements Iterable<T> {
    private final Enumeration<T> enumeration;

    public IterableEnumeration(final Enumeration<T> e) {
        enumeration = e;
    }

    public Iterator<T> iterator() {
        return new Iterator<T>() { 
             public boolean hasNext() {
                return (enumeration.hasMoreElements());
            }

            public T next() {
                return (enumeration.nextElement());
            }

            public void remove() {
                throw new UnsupportedOperationException("Cannot remove via an Enumeration");
            }
        };
    }
    // As suggested by http://www.javaspecialists.eu/archive/Issue107.html
    // but doesn't help with: final IterableEnumeration<ZipEntry> iteratable = IterableEnumeration.make( zipFile.entries() );
    public static <T> Iterable<T> make(Enumeration<T> en) {
        return new IterableEnumeration<T>(en);
    }
}

我想了解它!

最佳答案
我不确定foreach循环会发生什么,但是您需要将通配添加到IterableEnumeration的声明中,以接受从ZipFile.entries()返回的未指定类型.

更换

    private final Enumeration<T> enumeration;

    public IterableEnumeration(final Enumeration<T> e) {
        enumeration = e;
    }

    public static <T> Iterable<T> make(Enumeration<T> en) {
        return new IterableEnumeration<T>(en);
    }

    private final Enumeration<? extends T> enumeration;

    public IterableEnumeration(final Enumeration<? extends T> e) {
        enumeration = e;
    }

    public static <T> Iterable<T> make(Enumeration<? extends T> en) {
        return new IterableEnumeration<T>(en);
    }

猜你在找的Java相关文章