英文:
Using Type Parameters Created in One Class in a Different Class
问题
我在其中一个类(Table
)中定义了两个类型参数,T
和 G
。这个 Table
类正在使用一个 LinkedList
。我还创建了另一个类(Node
)用于 LinkedList
的节点,并定义了一些设置器和获取器。我想在制作 LinkedList
节点的 Table
类中使用相同的 T
和 G
。然而,我遇到了问题。
如何在不同的类中使用相同的类型参数?
public class Table<T, G> {
...
}
public class Node {
public T get(G g) {
...
}
}
英文:
I defined two type parameters, T
and G
, in one of my classes (Table
). This Table
class is using a LinkedList
. I have made another class (Node
) for the nodes of the LinkedList
with a few setters and getters. I would like to use the same T
and G
in my Table
class; which is making the LinkedList
nodes. However, I am running into issues.
How do I use the same type parameters in a different class?
public class Table<T, G> {
...
}
public class Node {
public T get(G g) {
...
}
}
答案1
得分: 3
Node也需要声明类型...
public class Table<T, G> {
private List<Node<T, G>> list = new ArrayList<>();
}
public class Node<T, G> {
public T get(G g) { ... }
}
英文:
Node needs to declare the types too...
public class Table<T, G> {
private List<Node<T, G>> list = new ArrayList<>();
}
public class Node<T, G> {
public T get(G g) { ... }
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论