Java 如何在内部类中使用参数化泛型类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2009809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I use paramertized generic types in an inner class?
提问by Will M
I am trying to implement an inner class that has a generic parameterized type.
我正在尝试实现一个具有泛型参数化类型的内部类。
Here is a short version of my code:
这是我的代码的简短版本:
public class AVLTree<T extends Comparable<? super T>> implements Iterable<T> {
...
private class BinaryNode<T extends Comparable<? super T>> {
...
}
private class TreePreOrderIterator<E extends Comparable<? super E>> implements Iterator<E> {
...
}
}
It does not work. Eclipse/Java is giving me a warning that the type T
parameter on the inner class is 'hiding' the super class's parameter. Any thoughts on how I can fix this?
这是行不通的。Eclipse/Java 警告我T
内部类的类型参数“隐藏”了超类的参数。关于如何解决这个问题的任何想法?
EDIT: I added the other inner class I'm having problems with: TreePreOrderIterator
. The generic type T
will be the same for AVLTree
, BinaryNode
, and TreePreOrderIterator
. The inner classes need to access fields in AVLTree.
编辑:我加我有问题的其他内部类:TreePreOrderIterator
。通用型T
将是相同的AVLTree
,BinaryNode
和TreePreOrderIterator
。内部类需要访问 AVLTree 中的字段。
EDIT2: Also, the Iterator
accesses a BinaryNode<T>
, which is a conflict.
EDIT2:另外,Iterator
访问 a BinaryNode<T>
,这是一个冲突。
(Note: This is part of bigger project I'm doing for a class. If any other information is needed, please ask.)
(注意:这是我为一个班级做的更大项目的一部分。如果需要任何其他信息,请询问。)
采纳答案by ILMTitan
If you want T
in BinaryNode
to be the same type as the enclosing T
associated with AVLTree
, remove the declaration of T
in BinaryNode
.
如果您希望T
in与与 关联BinaryNode
的封闭类型相同,请删除in的声明。T
AVLTree
T
BinaryNode
If you want the T
in BinaryNode
to be different than the enclosing T
associated with AVLTree
, but you
want to be able to access properties
of the parent AVLTree
, rename T
to
something else.
如果您希望T
inBinaryNode
不同于与T
关联的封闭AVLTree
,但您希望能够访问 parent 的属性AVLTree
,请重命名T
为其他名称。
If you don't need to access
properties of the enclosing AVLTree
,
make BinaryNode
static.
如果您不需要封闭的访问属性AVLTree
,化妆BinaryNode
静态的。
回答by notnoop
Just rename T
to something else so it wouldn't conflict with the outer class T.
只需重命名T
为其他名称,这样它就不会与外部类 T 发生冲突。
The type parameter names are visible in all the class scope, including the inner classes. So inner classes or instance methods should declare yet another type parameter whose name conflict with the class type parameter. However, static nested classes and static methods don't have that problem.
类型参数名称在所有类范围内都是可见的,包括内部类。所以内部类或实例方法应该声明另一个类型参数,其名称与类类型参数冲突。但是,静态嵌套类和静态方法没有这个问题。
回答by Edmund
It's because the parameter T
(as the parameter for the BinaryNode<T>
class) is the same as that used for AVLTree<T>
-- the parameter used for AVLTree
is normally in scope for the definition of the inner class BinaryNode
, but because you're giving a new parameter the same name, it will be hidden. You could instead use:
这是因为参数T
(作为BinaryNode<T>
类的参数)与用于AVLTree<T>
的参数相同——用于的参数AVLTree
通常在内部类定义的范围内BinaryNode
,但因为您给了一个新参数相同的名称,它将被隐藏。您可以改为使用:
private class BinaryNode<U extends Comparable<? super T>> { ... }
回答by Vort3x
Just remove generic type declaration from the BinaryNode class. Since the class is declared private, it can only exist within AVLTree, and therefor you can just use the T declared for AVLTree anywhere inside BinaryNode.
只需从 BinaryNode 类中删除泛型类型声明。由于该类被声明为私有,它只能存在于 AVLTree 中,因此您可以在 BinaryNode 内的任何位置使用为 AVLTree 声明的 T。