2014-10-19 107 views
0

我從JUNG庫中獲得了Hypergraph實現的接口和類。我擴展了Hypergraph接口來創建接口ISimpleHypergraph,以包含一些新方法,然後通過擴展類SetHypergraph和實現ISimpleHypergraph來創建一個新類SimpleHypergraph。如何在Java泛型中使用自定義類型

我還創建了具有id和權重字段的自定義SimpleV和SimpleH類型。現在我怎麼能在SimpleHypergraph中使用id字段實現幾個方法? 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 
} 
+1

研究通用邊界。另外,Java中沒有「實現」關鍵字。 – 2014-10-19 23:03:20

+0

我正在尋找界限,但並沒有完全明白。你能否提供一些提示。 – joarderm 2014-10-19 23:36:25

回答

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

避免能夠獲取ID。改用getter和setter。

您可能會使您的界面SimpleHypergraph通用,但我認爲它在您的情況下是多餘的。

+0

謝謝。有效 !!雖然我失去了通用的風格:( – joarderm 2014-10-20 01:22:34

+0

@jmkam你可能需要,如果你要擴展SimpleH/SimpleV並使用SimpleHypergraphImpl的一些功能,例如,我可以給你一個例子,如果你真的那樣。 – Multisync 2014-10-20 01:30:25

+0

一個例子是真的很有幫助 – joarderm 2014-10-20 01:46:50

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 IMPL EMENT HasId接口

從現在開始,你應該使用公共/保護的訪問修飾符的類屬性在裏面SimpleHypergraph

+0

我無權訪問Hypergraph界面。你的意思是接口ISimpleHypergraph? – joarderm 2014-10-19 23:35:43

+0

對不起,我忽略了'Hypergraph'接口在第三方庫中。改變'SimpleHypergraph'聲明。我編輯了答案來反映這一點。 – Alinoe 2014-10-20 00:18:42