0

我制作了一个使用 JGraphT 的界面。我的预期用途是Comparable,因为实现Comparable允许对象与某些数据结构一起使用。同样,我有一个 JGraphT 函数,我想使用它来处理任何Distanceable.

public interface Distanceable<E> {

    /**
     * A representation of the distance between these two objects.
     * If the distance between a0 and a1 is undefined, <code>a0.hasEdge(a1)</code> should return false;
     * @param o
     * @return
     */
    public int distance(E o);

    /**
     * Are these two objects connected?
     * @param o
     * @return True if the two objects are connected in some way, false if their distance is undefined
     */
    public boolean hasEdge(E o);
}

这是我在 JGraphtUtilities 中的 JGraphT 函数。它不是为 定义的Animal,而是为Distanceable

public static <E extends Distanceable> WeightedGraph<E, DefaultWeightedEdge> graphOfDistances(Set<E> nodes) {
    WeightedGraph<E, DefaultWeightedEdge> g = new SimpleWeightedGraph<E, DefaultWeightedEdge>(DefaultWeightedEdge.class);

    for (E a : nodes) {
        g.addVertex(a);
    }

    for (E a : nodes) {
        for (E a1 : nodes) {
            if (a.hasEdge(a1)) {
                g.addEdge(a, a1);
                g.setEdgeWeight(g.getEdge(a, a1), a.distance(a1));
            }
        }
    }

    return g;
}

但它不起作用。编译器在调用此方法的另一个类中的这一行上产生错误:

WeightedGraph<Animal, DefaultWeightedEdge> graphOfAnimals = JGraphtUtilities.graphOfAnimals(zoo);

错误是:

The method graphOfAnimals(Set<Animal>) is undefined for the type JGraphtUtilities

然而,

public class Animal implements Distanceable<Animal> {

我在这里做错了什么?

另一个问题:编译器给出了这个警告:

Distanceable is a raw type. References to generic type Distanceable<E> should be parameterized.

Distanceable如果我想让这个函数适用于所有对象,我想给它什么类型?

4

1 回答 1

2

graphOfAnimals(Set<Animal>) JGraphtUtilities 类型的方法未定义

您在代码示例中显示的方法是graphOfDistances. 问题出在方法graphOfAnimals上。所以...

你有一个在 课堂graphOfAnimals上接受 a 的方法吗?Set<Animal>JGraphtUtilities

于 2009-12-02T20:32:08.467 回答