2014-10-19 3 views
0

Jung 라이브러리의 Hypergraph 구현을위한 인터페이스 및 클래스는 다음과 같습니다. 인터페이스 Hypergraph를 확장하여 ISimpleHypergraph 인터페이스를 생성하여 몇 가지 새로운 메서드를 포함시킨 다음 SetHypergraph 클래스를 확장하고 ISimpleHypergraph를 구현하여 SimpleHypergraph 클래스를 새로 만들었습니다.Java generics에서 사용자 정의 유형을 사용하는 방법

또한 id 및 weight 필드가있는 사용자 지정 SimpleV 및 SimpleH 형식을 만들었습니다. 이제 SimpleHypergraph에서 id 필드를 사용하는 몇 가지 방법을 어떻게 구현할 수 있습니까? 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 
} 
+1

일반 경계를 살펴보십시오. 또한 Java에는'implementation' 키워드가 없습니다. –

+0

나는 경계를 정의하는 것을보고 있었지만 그것을 얻지는 못했습니다. 당신은 힌트를 줄 수 있습니까? – joarderm

답변

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

않도록 내부에 수 GET ID를해야합니다 지금부터 HasId 인터페이스

을 장담. getter와 setter를 대신 사용하십시오.

인터페이스 SimpleHypergraph를 일반적인 것으로 만들 수도 있지만 귀하의 경우 중복되어 있다고 생각합니다.

+0

감사. 그것은 일했다!! 비록 일반적인 맛을 잃어 버렸지 만 : ( – joarderm

+0

@jmkam SimpleH/SimpleV를 확장하고 SimpleHypergraphImpl에서 일부 기능을 사용하려면이 기능이 필요할 수 있습니다. 예를 들어,이 기능을 사용할 수 있습니다. – Multisync

+0

예를 들어 도움이됩니다. – joarderm

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 확인 당신은 클래스 속성 공공/보호 액세스 한정자를 사용하여 SimpleHypergraph

+0

Hypergraph 인터페이스에 액세스 할 수 없습니다. ISimpleHypergraph 인터페이스를 의미합니까? – joarderm

+0

죄송합니다. 'Hypergraph'인터페이스가 타사 라이브러리에 있다는 것을 간과했습니다. 'SimpleHypergraph' 선언을 변경하십시오. 나는 그것을 반영하기 위해 답을 편집했다. – Alinoe

관련 문제