使用在一个类中创建的类型参数在另一个类中。

huangapple go评论88阅读模式
英文:

Using Type Parameters Created in One Class in a Different Class

问题

我在其中一个类(Table)中定义了两个类型参数,TG。这个 Table 类正在使用一个 LinkedList。我还创建了另一个类(Node)用于 LinkedList 的节点,并定义了一些设置器和获取器。我想在制作 LinkedList 节点的 Table 类中使用相同的 TG。然而,我遇到了问题。

如何在不同的类中使用相同的类型参数?

  1. public class Table<T, G> {
  2. ...
  3. }
  4. public class Node {
  5. public T get(G g) {
  6. ...
  7. }
  8. }
英文:

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?

  1. public class Table&lt;T, G&gt; {
  2. ...
  3. }
  4. public class Node {
  5. public T get(G g) {
  6. ...
  7. }
  8. }

答案1

得分: 3

Node也需要声明类型...

  1. public class Table<T, G> {
  2. private List<Node<T, G>> list = new ArrayList<>();
  3. }
  4. public class Node<T, G> {
  5. public T get(G g) { ... }
  6. }
英文:

Node needs to declare the types too...

  1. public class Table&lt;T, G&gt; {
  2. private List&lt;Node&lt;T, G&gt;&gt; list = new ArrayList&lt;&gt;();
  3. }
  4. public class Node&lt;T, G&gt; {
  5. public T get(G g) { ... }
  6. }

huangapple
  • 本文由 发表于 2020年9月24日 09:45:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/64038414.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定