【发布时间】:2017-07-24 03:02:22
【问题描述】:
我有一个实现 Iterable 的类,以便用户可以使用迭代器。我使用泛型来允许用户使用任何类型并使用该类。
这是下面没有警告的工作代码-
public class CustomStackUsingArray<Type> implements CustomList<Type>, Iterable<Type> {
Type []arr;
public Iterator<Type> iterator() {
return (new ListIterator());
}
private class ListIterator implements Iterator<Type>{
//Implement Iterator here
}
//Implement other methods here
}
但是,如果我有这样定义的 ListIterator-
private class ListIterator<Type> implements Iterator<Type>
我在 Eclipse 中收到警告,The parameter Type is hiding the type Type
为什么我在课后指定泛型类型时会报错?我不应该这样做才能在课堂上使用 Type 吗?我在定义CustomStackUsingArray 时添加了Type,效果很好。
【问题讨论】:
-
这在某处有重复,但是当您重新声明它时,您从顶级泛型中隐藏了
<Type>参数。 -
如果你认为你必须说
class ListIterator<Type>才能在你的内部类中使用Type——不,你不需要。