2017-01-16 21 views
0

特殊なツリーを作成するためのインターフェイスが1つあります(BSTNode)。クラスを作成するには、汎用クラスを実装するのですか?

public interface BSTNode<K extends Comparable<K>, V> { 
    /** 
    * Recovers the value stored in the node 
    * @return the value stored in the node 
    */ 
    public V getValue(); 

    /** 
    * Sets the value stored in the node 
    * @param value the value to store in the node 
    */ 
    public void setValue(V value); 

    /** 
    * Recovers the key stored in the node 
    * @return the key stored in the node 
    */ 
    public K getKey(); 

    /** 
    * Sets the key stored in the node 
    * @param key the key to store in the node 
    */ 
    public void setKey(K key); 

    /** 
    * Recover the parent stored of the current node 
    * @return the parent of the current node 
    */ 
    public BSTNode<K, V> getParent(); 

    /** 
    * Set the parent of the current node 
    * @param parent to set for the current node 
    */ 
    public void setParent(BSTNode<K, V> parent); 
    } 

しかし、私はいくつかの問題に遭遇し、このインタフェースを実装する場合:それは

は、誰が私にこのインタフェースを実装するための正しい方法を伝えることができるV cannot be resolved to a type - K cannot be resolved to a typeことを私に思い出させ続け

public abstract class BinarySearchTreeNode implements BSTNode { 

    private Object value; 
    private Object key; 
    private BinarySearchTreeNode parent; 
    private BinarySearchTreeNode left; 
    private BinarySearchTreeNode right; 

    public BinarySearchTreeNode(){ 
     this.value=null; 
     this.key=null; 
     this.parent=null; 
     this.left=null; 
     this.right=null; 
    } 

    public BinarySearchTreeNode(Object value, Object key, BinarySearchTreeNode parent, BinarySearchTreeNode left, BinarySearchTreeNode right){ 
     this.value=value; 
     this.key=key; 
     this.parent=parent; 
     this.left=left; 
     this.right=right; 
    } 


    public Object getValue() { 
     return this.value; 
    } 


    public void setValue(Object value) { 

    } 


    public Comparable getKey() { 
     return null; 
    } 


    public void setKey(Comparable key) { 
    } 


    public BSTNode<K, V> getParent() { 
    } 


    public void setParent(BSTNode<K, V> parent); 

答えて

1

はい、VおよびKタイプのパラメータは、サブクラスの独自のタイプに置き換えてください。サンプルインスタンスでは、次のコードセグメントを確認してください。

public interface Transformer<S, T> { 
    T transform(S source); 

} 

public class DocumentToStudentTransformer implements Transformer<Document, BaseStudentDTO> { 

    private static final String ID = "_id"; 
    public static final String STREAM = "stream"; 
    public static final String GPA = "gpa"; 
    public static final String AGE = "age"; 
    public static final String NAME = "name"; 

    @Override 
    public StudentDTO transform(Document source) { 
     return new StudentDTO(source.getString(NAME), source.getInteger(AGE), source.getDouble(GPA), 
       source.getString(STREAM), ((ObjectId) source.get(ID)).toString()); 
    } 
} 

これが役に立ちます。ハッピーコーディング!

+0

詳細な回答ありがとうございます。私は 'K'を置き換えるためにどのタイプを使うべきですか、私はコンパレータを試しましたが、全く動作しません。 –

+0

あなたはインターフェイス契約を定義した人なので、それを知っておく必要があります。 Kはノードに格納されているキーを意味し、Vはツリーノードに格納されている値を意味します。これらの構造を前もって定義しておくべきです。難しいものではないJavaの型パラメータとジェネリックスについての記事をご覧ください。 –

関連する問題