这里有两种形式可以使用扩展基类型的泛型:
public abstract <T extends Runnable> BlockingQueue<T> getQueueA();
public abstract BlockingQueue<? extends Runnable> getQueueB();
我不明白。有什么不同?这两种方法似乎都返回扩展 Runnable 对象的相同 BlockingQueue。
最佳答案
BlockingQueue<Thread> threads = thing.getQueueA(); // works
BlockingQueue<Thread> threads = thing.getQueueB(); // does not work
另一方面:
@Override public <T extends Runnable> BlockingQueue<T> getQueueA() {
return new ArrayBlockQueue<Thread>(); // no worky
}
@Override public BlockingQueue<? extends Runnable> getQueueB() {
return new ArrayBlockQueue<Thread>(); // works
}
关于java - 需要泛型转换解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21206073/