我有以下JUNG库中Hypergraph实现的接口和类。我扩展了接口Hypergraph以创建接口ISimpleHypergraph,使其包含一些新方法,然后通过扩展类SetHypergraph并实现ISimpleHypergraph来创建新的类SimpleHypergraph。

我还创建了具有id和weight字段的自定义SimpleV和SimpleH类型。现在如何使用id字段在SimpleHypergraph中实现一些方法?在SimpleHypergraph内部,无法识别SimpleV和SimpleH。有什么建议或更好的方法吗?

请注意,接口Hypergraph和类SetHypergraph是JUNG libray的一部分。



public interface Hypergraph<V, H> {
    // Other definitions
}




public interface ISimpleHypergraph<V, H> extends Hypergraph<V, H> {
    H get(int id);
    H get(Set<V> vSet);

    // Other definitions
}




public class SetHypergraph<V, H> implements Hypergraph<V, H> {
    protected Map<H, Set<V>> edges;
    // Other fields

    public SetHypergraph() {
        edges = new HashMap<H, Set<V>>();
    }

    // Other methods
}




public class SimpleHypergraph<V, H> extends SetHypergraph<V, H> implements ISimpleHypergraph<V, H> {

    public H get(int id) {
        // How to use SimpleH.id and SimpleV.id here to get the
        // searched Key entry from the Map<H, Set<V>> edges
    }

    public H get(Set<V> vSet) {
        // How to use SimpleH.id and SimpleV.id here to get the
        // searched Key entry from the Map<H, Set<V>> edges
    }
}




public class SimpleV {

    public int id;
    public int weight;

    public SimpleV(int id, int weight) {
        this.id = id;
        this.weight = weight;
    }

    // Other methods
}




public class SimpleH {

    public int id;
    public int weight;

    public SimpleH(int id, int weight) {
        this.id = id;
        this.weight = weight;
    }

    // Other methods
}

最佳答案

public interface Hypergraph<V, H> {
    // Other definitions
}

...

public class SetHypergraph<V, H> implements Hypergraph<V, H> {
    protected Map<H, Set<V>> edges;
    // Other fields

    public SetHypergraph() {
        edges = new HashMap<H, Set<V>>();
    }

    // Other methods
}

...

public interface SimpleHypergraph<V extends SimpleV, H extends SimpleH> extends Hypergraph<V, H> {
    H get(int id);
    H get(Set<V> vSet);
}


...

public class SimpleHypergraphImpl<V extends SimpleV, H extends SimpleH> extends SetHypergraph<V, H> implements SimpleHypergraph<V, H> {

    public H get(int id) {
        // your code
        return null;
    }

    public H get(Set<V> vSet) {
        // your code (V is at least SimpleV, so you can use its accessible properties/methods here
        return null;
    }

    // example of usage
    public static void main(String[] args) {
        SimpleHypergraph<SimpleV, SimpleH> simpleHyperGraph = new SimpleHypergraphImpl<SimpleV, SimpleH>();
        Set<SimpleV> set = new HashSet<SimpleV>();
        set.add(new SimpleV(1,1));
        set.add(new ComplicatedV(1000,1000));
        SimpleH h = simpleHyperGraph.get(0);
        h = simpleHyperGraph.get(set);
    }
}


...

public class SimpleV {

    private int id;
    private int weight;

    public SimpleV(int id, int weight) {
        this.id = id;
        this.weight = weight;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    // Other methods
}

...

public class SimpleH {

    private int id;
    private int weight;

    public SimpleH(int id, int weight) {
        this.id = id;
        this.weight = weight;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    // Other methods
}

public class ComplicatedV extends SimpleV {

    public ComplicatedV(int id, int weight) {
        super(id, weight);
    }
}


避免对类属性使用公共/受保护的访问修饰符。请改用getter和setter。

您可以使您的接口SimpleHypergraph通用,但我认为这在您的情况下是多余的。

09-30 13:28