简体   繁体   English

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

[英]How to use custom types in Java generics

I have the below interfaces and classes for Hypergraph implementations from JUNG library. 我有以下JUNG库中Hypergraph实现的接口和类。 I extended interface Hypergraph to create interface ISimpleHypergraph to include few new methods and then create a new class SimpleHypergraph by extending class SetHypergraph and implementing ISimpleHypergraph. 我扩展了接口Hypergraph以创建接口ISimpleHypergraph,使其包含一些新方法,然后通过扩展类SetHypergraph并实现ISimpleHypergraph来创建新的类SimpleHypergraph。

I also created custom SimpleV and SimpleH types which have id and weight fields. 我还创建了具有id和weight字段的自定义SimpleV和SimpleH类型。 Now how can I implement few methods in SimpleHypergraph utilising the id fields? 现在如何利用id字段在SimpleHypergraph中实现一些方法? Inside SimpleHypergraph SimpleV and SimpleH are not recognizable. 在SimpleHypergraph内部,无法识别SimpleV和SimpleH。 Any suggestions or even better way to do this? 有什么建议甚至更好的方法吗?

Note that, interface Hypergraph and class SetHypergraph are part of the JUNG libray. 请注意,接口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
}

Create an interface like: 创建一个如下界面:

public interface HasId {
    int getId()
}

and change declaration 并更改声明

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

to

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

Make SimpleV and SimpleH implement HasId interface 使SimpleVSimpleH实现HasId接口

from now on you should be able get id inside SimpleHypergraph 从现在开始,您应该可以在SimpleHypergraph获取ID

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);
    }
}

Avoid using public / protected access modifiers for class properties. 避免对类属性使用公共/受保护的访问修饰符。 Use getters and setters instead. 请改用getter和setter。

You may make your interface SimpleHypergraph generic but I think it's redundant in your case. 您可以使您的接口SimpleHypergraph通用,但我认为这对您来说是多余的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM