Generic class using generic parameter

I want to achieve the following.

I have a general class Node<K,T,V>that looks like this:

public class Node<K,T,V>{

/**
 * @return the key
 */
public K getKey() {
    return key;
}
/**
 * @param key the key to set
 */
public void setKey(K key) {
    this.key = key;
}
// etc...

Now I want to have a class Treethat works with nodes with arbitrary parameterized types:

public class Tree <Node<K,V,T>> {
   public void insert(Node<K,V,T> node){
      K key = node.getKey();
      // do something...
   }
// ... etc...
}

This, however, does not work, as Eclipse tells me that the line public class Tree <Node<K,V,T>>does not look very good :) If I change it to

public class Tree <Node> {

He tells me that the type of Node hides the type of Node. How can I achieve this from the Tree class, can I refer to types K, V and T correctly?

I am sure that basillions have answered this question once. However, I did not find anything - sorry for that !!!

Greetings.

+4
source share
1 answer

:

class Tree<K, T, V> {
    public void insert(Node<K, T, V> node) {
        K key = node.getKey();
        // do something...
    }
    // ... etc...
}

Node,

class Tree2<N extends Node<K, T, V>, K, T, V> {
    public void insert(N node) {
        K key = node.getKey();
        // do something...
    }
    // ... etc...
}
+6

Source: https://habr.com/ru/post/1547712/


All Articles