下面是JUNG库中超图实现的接口和类。我扩展了接口超图以创建接口ISimpleHypergraph以包含很少的新方法,然后通过扩展类SetHypergraph和实现ISimpleHypergraph来创建一个新的类SimpleHypergraph。
我还创建了自定义的SimpleV和SimpleH类型,它们具有id和have字段。现在,我如何在SimpleHypergraph中实现几个使用id字段的方法?在SimpleHypergraph内部,SimpleV和SimpleH是不可识别的。有什么建议或者更好的方法吗?
注意,接口超图和类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
}发布于 2014-10-20 00:27:15
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泛化,但我认为在您的情况下它是多余的。
发布于 2014-10-19 23:24:42
创建一个接口,如:
public interface HasId {
int getId()
}和变更声明
public class SimpleHypergraph<V, H> extends SetHypergraph<V, H> implements ISimpleHypergraph<V, H>至
public class SimpleHypergraph<V extends HasId, H extends HasId> extends SetHypergraph<V, H> implements ISimpleHypergraph<V, H>使SimpleV和SimpleH实现HasId接口
从现在开始,您应该能够在SimpleHypergraph中获得id。
https://stackoverflow.com/questions/26456364
复制相似问题