在 java 中实例化递归定义的复合泛型 类

instantiate the recursively defined composite generic classes in java

我定义 类 Graph containing GraphNode 如下,我声明 N 的目的是使用泛型比较 2 个 GraphNode 对象。

问题是如何实例化递归绑定的Graph。

如下声明时出错。

Graph<Integer,Comparable<GraphNode>> graph = new Graph<>();

绑定不匹配:类型 Comparable<GraphNode> 不是类型 Graph<T,N>

的绑定参数 <N extends Comparable<GraphNode<T,N>>> 的有效替代
public class Graph<T, N extends Comparable<GraphNode<T, N>>> {

    private N root;

    private Class<N> clazz;

    Graph(Class<N> clazz) {
        this.clazz = clazz;
    }

    public N getInstance() {
        try {
            return clazz.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
            return null;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            return null;
        }
    }

    @SuppressWarnings("unchecked")
    public void insert(T d, N n) {
        if (root == null && n == null)
            root = getInstance();
        if (root == null)
            root = n;
        N node = root;
        while (node != null) {
            if (node.equals(n)) {
                N newNode = getInstance();
                ((GraphNode<T, N>) newNode).setAdjNode(newNode);
            }
        }
    }

}

public class GraphNode<T, N extends Comparable<GraphNode<T, N>>> implements Comparable<N> {

    private T data;

    private LinkedHashSet<N> adjNodes = new LinkedHashSet<>();

    GraphNode() {
        data = null;
    }

    GraphNode(T d) {
        setData(d);
    }

    public void setAdjNode(N n) {
        adjNodes.add(n);
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public int hashCode() {
        return data.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof GraphNode<?, ?>) {
            return ((GraphNode<?, ?>) obj).getData() == this.getData();
        }
        return false;
    }

    @Override
    public String toString() {
        return data + "";
    }

    @Override
    public int compareTo(N o) {
        return this.compareTo(o);
    }

}

这解决了我上面的问题

 public class Graph<T extends Comparable<T>> {

    private GraphNode<T> root;

    public void insert(T d, GraphNode<T> n) {
        if (root == null && n == null)
            root = new GraphNode<T>(d);
        if (root == null)
            root = n;
        GraphNode<T> node = root;
        Queue<GraphNode<T>> queue = new ConcurrentLinkedQueue<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            node = queue.poll();
            node.setNodeColor(color.BLACK);
            if (node.equals(n)) {
                GraphNode<T> newNode = new GraphNode<T>(d);
                ((GraphNode<T>) newNode).setAdjNode(newNode);
            } else {
                queue.addAll(node.getUnexploredAdjNodes());
            }
        }
    }

}
public class GraphNode<T extends Comparable<T>> implements Comparable<GraphNode<T>> {

    enum color {
        WHITE, GREY, BLACK
    };

    private T data;

    private color nodeColor = color.WHITE;

    private LinkedHashSet<GraphNode<T>> adjNodes = new LinkedHashSet<>();

    GraphNode() {
        data = null;
    }

    GraphNode(T d) {
        setData(d);
    }

    public void setAdjNode(GraphNode<T> n) {
        adjNodes.add(n);
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public LinkedHashSet<GraphNode<T>> getAdjNodes() {
        return adjNodes;
    }

    public LinkedHashSet<GraphNode<T>> getUnexploredAdjNodes() {
        LinkedHashSet<GraphNode<T>> n = new LinkedHashSet<>();
        for (GraphNode<T> node : adjNodes) {
            if (node.getNodeColor() == color.WHITE)
                n.add(node);
        }
        return n;
    }

    public color getNodeColor() {
        return nodeColor;
    }

    public void setNodeColor(color nodeColor) {
        this.nodeColor = nodeColor;
    }

    @Override
    public int hashCode() {
        return data.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof GraphNode<?>) {
            return ((GraphNode<?>) obj).getData() == this.getData();
        }
        return false;
    }

    @Override
    public int compareTo(GraphNode<T> o) {
        return data.compareTo(o.data);
    }

    @Override
    public String toString() {
        return data + "";
    }
}
Graph<Integer> graph = new Graph<>();