gpt4 book ai didi

java - 如何让内部类继承封闭类的泛型类型?

转载 作者:太空狗 更新时间:2023-10-29 22:46:36 41 4
gpt4 key购买 nike

我正在使用 Java 6。

我无法让我的内部类使用与其封闭类相同的泛型类。目前我有

public class TernarySearchTree < T > {
...
protected class TSTNode < T > {
// index values for accessing relatives array
protected static final int PARENT = 0, LOKID = 1, EQKID = 2, HIKID = 3;
protected char splitchar;
protected TSTNode < T > [] relatives;
private T data;

protected TSTNode(char splitchar, TSTNode < T > parent) {
this.splitchar = splitchar;
relatives = new TSTNode[4];
relatives[PARENT] = parent;
}
}
}

现在我收到警告

The type parameter T is hiding the type T

如果我从内部类中删除类型参数(即从 <T> 行中删除 protected class TSTNode<T>),那么我会在 relatives = new TSTNode[4] 行上遇到编译错误.

我怎样才能把一切都做好?

最佳答案

您可以:

  • 删除 <T>来自 TSTNode 的类型参数(即,使其成为非通用的)——它仍然可以访问外部 <T> .

  • 重命名 <T>类中的类型参数 TSTNode到(说)U .

[更新]

以下是重写代码的四种不同方法。他们都编译。我认为你应该考虑使用 EnumMap (见下文第 4 版)。

版本 1:在内部类中使用不同名称的类型参数。您需要使用列表而不是数组。

  public class TernarySearchTree<T> {

protected class TSTNode<U> {
// index values for accessing relatives array:
protected static final int PARENT = 0, LOKID = 1, EQKID = 2, HIKID = 3;

protected char splitchar;
protected List<TSTNode<U>> relatives;
private U data;

protected TSTNode(char splitchar, TSTNode<U> parent) {
this.splitchar = splitchar;
relatives = new ArrayList<TSTNode<U>>();
for (int i = 0; i < HIKID; ++i) { // Allocate 4 slots in relatives
relatives.add(null);
}
relatives.set(PARENT, parent);
}
}

private TSTNode<T> node; // When you use it, pass T as U

public TernarySearchTree() {
node = new TSTNode<T>(',', null); // When you use it, pass T as U
}
}

版本 2:从封闭类继承 T

  public class TernarySearchTree<T> {

protected class TSTNode {
// index values for accessing relatives array:
protected static final int PARENT = 0, LOKID = 1, EQKID = 2, HIKID = 3;

protected char splitchar;
protected List<TSTNode> relatives;
private T data;

protected TSTNode(char splitchar, TSTNode parent) {
this.splitchar = splitchar;
relatives = new ArrayList<TSTNode>();
for (int i = 0; i < HIKID; ++i) { // Allocate 4 slots in relatives
relatives.add(null);
}
relatives.set(PARENT, parent);
}
}

private TSTNode node;

public TernarySearchTree() {
node = new TSTNode(',', null);
}
}

版本 3:使用 map (而不是列表)

  public class TernarySearchTree<T> {

protected class TSTNode {
// index values for accessing relatives array:
protected static final int PARENT = 0, LOKID = 1, EQKID = 2, HIKID = 3;

protected char splitchar;
protected Map<Integer, TSTNode> relatives;
private T data;

protected TSTNode(char splitchar, TSTNode parent) {
this.splitchar = splitchar;
// Create a hash map. No need to pre-allocate!
relatives = new HashMap<Integer, TSTNode>();
relatives.put(PARENT, parent); // set -> put
}
}

private TSTNode node;

public TernarySearchTree() {
node = new TSTNode(',', null);
}
}
}

版本 4:将索引定义为枚举 + 使用 EnunMap(而不是 HashMap )

  public class TernarySearchTree<T> {

protected static enum Index {
PARENT, LOKID, EQKID, HIKID;
}

protected class TSTNode {
protected char splitchar;
protected EnumMap<Index, TSTNode> relatives;
private T data;

protected TSTNode(char splitchar, TSTNode parent) {
this.splitchar = splitchar;
// Create an EnumMap.
relatives = new EnumMap<Index, TSTNode>(Index.class);
relatives.put(Index.PARENT, parent);
}
}

private TSTNode node;

public TernarySearchTree() {
node = new TSTNode(',', null);
}
}

[更新 2]要记住一件事:Use EnumMap instead of ordinal indexing

关于java - 如何让内部类继承封闭类的泛型类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8765211/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com