首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Java泛型中使用自定义类型

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

Stack Overflow用户
提问于 2014-10-19 23:01:42
回答 2查看 84关注 0票数 0

下面是JUNG库中超图实现的接口和类。我扩展了接口超图以创建接口ISimpleHypergraph以包含很少的新方法,然后通过扩展类SetHypergraph和实现ISimpleHypergraph来创建一个新的类SimpleHypergraph。

我还创建了自定义的SimpleV和SimpleH类型,它们具有id和have字段。现在,我如何在SimpleHypergraph中实现几个使用id字段的方法?在SimpleHypergraph内部,SimpleV和SimpleH是不可识别的。有什么建议或者更好的方法吗?

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

代码语言:javascript
复制
public interface Hypergraph<V, H> {
    // Other definitions
}

代码语言:javascript
复制
public interface ISimpleHypergraph<V, H> extends Hypergraph<V, H> {
    H get(int id);
    H get(Set<V> vSet);

    // Other definitions
}

代码语言:javascript
复制
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
}

代码语言:javascript
复制
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
    }
}

代码语言:javascript
复制
public class SimpleV {

    public int id;
    public int weight;

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

    // Other methods
}

代码语言:javascript
复制
public class SimpleH {

    public int id;
    public int weight;

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

    // Other methods
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-10-20 00:27:15

代码语言:javascript
复制
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泛化,但我认为在您的情况下它是多余的。

票数 1
EN

Stack Overflow用户

发布于 2014-10-19 23:24:42

创建一个接口,如:

代码语言:javascript
复制
public interface HasId {
    int getId()
}

和变更声明

代码语言:javascript
复制
public class SimpleHypergraph<V, H> extends SetHypergraph<V, H> implements ISimpleHypergraph<V, H>

代码语言:javascript
复制
public class SimpleHypergraph<V extends HasId, H extends HasId> extends SetHypergraph<V, H> implements ISimpleHypergraph<V, H>

使SimpleVSimpleH实现HasId接口

从现在开始,您应该能够在SimpleHypergraph中获得id。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26456364

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档