Using generic classes and generic methods in Java is difficult

Java novice, I did search some examples, but I'm really sad

I have a node class that uses generics:

public class Node<T> {

    private int key;
    private T data;
    private Node<T> nextNode;

}

The node is good

public class GenericDictionary implements GenericDictionary_interface {

    private int capacity;
    private Node<T> [] slots;

    public void add (Node<T> newNode) {

    }
}

I think that's the way I write it I want genericdictionary to work with node objects

I received an error: "t cannot resolve to type."

The thing is, I don't know what it should look like

Solution

Your genericdictionary class also needs to be generic:

public class GenericDictionary<T> ...
{
}

At that time, you can use node < T > In class.

If the interface contains the add method, the interface should also be generic:

public class GenericDictionary<T> implements Dictionary<T>
{
}

By the way, I don't recommend using a named genericdictionary_ Interface... If there is only one implementation, you can call the interface genericdictionary and implement genericdictionaryimpl - a little scary, but quite common If there are multiple implementations, please name the implementation through some distinguished aspects

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>