问题
I have an outer class (LinkedStack<T>
) that has an inner node class. Is it necessary to declare the inner Node class with the same generic like
private Node<T>
as opposed to
private Node
or does it not make any difference?
回答1:
If the inner class is a static class then yes, otherwise no.
I.e.:
class LinkedStack<T> {
// references to T refer to LinkedStack's T.
static class Node<T> {
// references to T refer to Node's T.
T data;
}
// ...
Node<T> node;
}
or:
class LinkedStack<T> {
// references to T refer to LinkedStack's T.
class Node {
// references to T refer to LinkedStack's T.
T data;
}
// ...
Node node;
}
来源:https://stackoverflow.com/questions/42817415/generic-type-in-inner-class