如何在递归generics类中创建有效对象?

问题

我正在树类型数据结构上实现递归add()方法,使用表示树节点的generics类实现。

如果子树是generics类型,如何在该递归中创建子树 – 因此没有我可以调用的构造函数?

情况详情:

我有一个generics类集(下面是一个地图),旨在实现对象的地图(基本上是N级树)的地图的映射。

public interface NodeInterface { public void add(List, ValueType); public ValueType getSubTree(KeyType); // Get subtree map } // Generic map class. // Leaf level nodes are maps too, just not maps containing other maps public abstract class NodeMap  implements NodeInterface { protected Map map; // actual map ... } // Leaf level objects - map NOT containing other maps public class LeafNode extends NodeMap  { public void add(List, ValueType) {} // implementation for leaf add ... } // Tree nodes (non-leaf) public class ParentNodeMap <ValueType extends NodeMap > extends NodeMap  { // This class is needed because there are methods that must be // implemented on a map that can't be implemented on leaf level nodes. ... } // Actual tree node classes (needed as syntactic sugar, to avoid end users // having to deal with HashMap<String, Hashmap<String, HashMap>>>> public class Level2Node extends ParentNodeMap  { } public class Level1Node extends ParentNodeMap  { } 

需要做什么:

我正在尝试实现一个方法,它将递归地将叶级对象插入到这个地图映射图中(在界面中描述)。

例如,期望的行为是:

 tree = new Level1Node(); List keys = Arrays.asList("key1", "key2"); // I think I need 3 here? irrelevant. MyDataType data = new MyDataType(); tree.add(keys, data); 

问题:

我似乎无法在ParentNode的add()方法中实现子树映射的创建和插入

这是我在ParentNodeMap类中尝试的:

 public void add(List keys, ValueType value); KeyType key = getFirstKey(keys); createMapIfNull(); // creates "map" as new HashMap() ValueType subTree = getSubTree(key); // Gets me child subtree from map // PROBLEM! that subtree is now "null" since we just created an new Map // which has no key "hey" // Now here's where we should create a new subtree and add it: if (subTree == null) { subTree = new ValueType(); // THIS IS WHAT FAILS!!!! map.put(key, subTree); subTree.add(keys.removeFirstKey(), value); // recursively add to subtree } 

现在,我特别坚持的是:如何创建一个subTree对象(NodeMap类型); 其类型是通用的ValueType extends NodeMap ,这就是我所知道的一切?

调用new ValueType()给出“无法实例化类型ValueType”错误。