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

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

Using Type Parameters Created in One Class in a Different Class

问题

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

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

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&lt;T, G&gt; {
        ...
}

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&lt;T, G&gt; {

    private List&lt;Node&lt;T, G&gt;&gt; list = new ArrayList&lt;&gt;();
    
}

public class Node&lt;T, G&gt; {

    public T get(G g) { ... } 
}

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:

确定