如何在Java泛型中使用自定义类型

时间:2014-10-19 23:01:42

标签: java generics jung

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

我还创建了具有id和weight字段的自定义SimpleV和SimpleH类型。现在我如何利用id字段在SimpleHypergraph中实现几个方法?在SimpleHypergraph中,SimpleV和SimpleH无法识别。有什么建议甚至更好的方法吗?

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


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
}

2 个答案:

答案 0 :(得分:1)

创建如下界面:

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>

SimpleVSimpleH实施HasId界面

从现在起,您应该可以在SimpleHypergraph

中获取ID

答案 1 :(得分:1)

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通用,但我认为在您的情况下这是多余的。