本文介绍了Java:结合了泛型,内部类和“实现"的问题;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了将泛型,implements和内部类组合在一起的问题.我正在创建一个包含内部类的LinkedBinaryHeap类.这个内部类是泛型HeapNode,它扩展了我创建的泛型Node类;它只是为键/优先级添加了变量和方法.

I am having an issue combining generics, implements, and inner classes.I am creating a LinkedBinaryHeap class which contains an inner class. This inner class is the generic HeapNode which extends the generic Node class I created; it just adds a variable and methods for a key/priority.

LinkedBinaryHeap中,我创建了一个通用LinkedList来存储HeapNode.我假设要存储的通用数据扩展了Comparable类.

In LinkedBinaryHeap I create a generic LinkedList to store HeapNodes.I am assuming the generic data being stored extends Comparable class.

以下是存储内容的布局:

Here is a layout of what stores what:

BinaryHeap->LinkedList(Nodes)->HeapNode(extends Node)->DATA,KEY

我的问题是声明LinkedList

My issue is that when declaring the LinkedList:

LinkedList<HeapNode> heap;

eclipse强调了HeapNode并给了我错误:

eclipse underlines HeapNode and gives me the error:

Bound mismatch: The type LinkedBinaryHeap.HeapNode is not a
valid substitute for the bounded parameter >
of the type LinkedList

认为该错误告诉我HeapNode必须实现Comparable,但是我的Node类却实现了Comparable,所以照顾好吗,对吗?

I think the error is telling me that HeapNode must implement the Comparable, however my Node class implements Comparable, so that is taken care of, correct?

我尝试了各种不同的方法,但是似乎没有任何效果,下面的代码是我刚接触的代码.请注意,我已经尝试将implements Comparable Node<T>离开HeapNode内部类,并且它什么都没有改变.

I have tried all sorts of different things, but nothing seems to work, the below code is the closest I came. Note that I have tried leaving implements Comparable Node<T> off the HeapNode inner class, and it changes nothing.

代码:

LinkedBinaryHeap.java:

LinkedBinaryHeap.java:

public class LinkedBinaryHeap<E extends Comparable<E>> {
    private LinkedList<HeapNode> heap;

    public LinkedBinaryHeap(){
        heap = new LinkedList<HeapNode>();
    }

    /* INNER CLASS DECLARATION. */
    private class HeapNode extends Node<E> implements Comparable<Node<E>>{
        int key;
        public HeapNode(int key, E data){
            super(data);
            this.key = key;
        }

        public int getKey(){
            return key;
        }

        public void setKey(int key){
            this.key = key;
        }
    }
}

Node.java:

Node.java:

public class Node<T extends Comparable<T>> implements Comparable<Node<T>>{
    protected T data;
    protected Node<T> next;
    protected Node<T> previous;

    public Node(T data){
        next = null;
        previous = null;
        this.data = data;
    }

    /* Some other methods left out here. */

    public int compareTo(Node<T> node) {
        return data.compareTo(node.getData());
    }
}

LinkedList.java:

LinkedList.java:

public class LinkedList<T extends Comparable<T>> implements Comparable<LinkedList<T>>{
    private Node<T> head;
    private Node<T> tail;
    private int size;

    public LinkedList(){
        head = null;
        tail = null;
        size = 0;
    }

    /* Other methods left out. */

    public int compareTo(LinkedList<T> list){
        // does stuff.
    }
}

推荐答案

根据您的定义:

  1. HeapNodeNode<E>的子类型,但implements Comparable<Node<E>>
  2. LinkedList需要一个类型自变量,以使T implements Comparable<T>
  3. LinkedList<HeapNode>要求HeapNode implements Comparable<HeapNode>
  4. 它不是(从上面的(1),它是implements Comparable<Node<E>>)
  1. HeapNode is a subtype of Node<E> but implements Comparable<Node<E>>
  2. LinkedList requires a type argument such that T implements Comparable<T>
  3. i.e. a LinkedList<HeapNode> requires that HeapNode implements Comparable<HeapNode>
  4. which it does not (from (1), above, it implements Comparable<Node<E>>)

所以两者不兼容.

LinkedList中,您需要将节点类型表示为适当限制的类型参数,以及节点类型的组件类型参数也适当限制的地址:

You need, in LinkedList, to express the node type as a type parameter, bounded appropriately, and the node type's component type parameter as well, also bounded appropriately:

public class LinkedList<N extends Node<E>,
                        E extends Comparable<E>>
  implements Comparable<LinkedList<N, E>>{
  private N head;
  private N tail;
  private int size;
  ...

现在您的LinkedBinaryHeap需要调整对LinkedList的使用:

Now your LinkedBinaryHeap needs to adjust it's use of LinkedList:

public class LinkedBinaryHeap<E extends Comparable<E>> {
  private LinkedList<HeapNode, E> heap;

  public LinkedBinaryHeap(){
      heap = new LinkedList<HeapNode, E>();
  }

现在应该可以编译了.它是否达到了将所有内容与其他所有内容进行比较的目的!

That should now compile. Whether it achieves your goals of comparing everything to everything else is harder to say!

这篇关于Java:结合了泛型,内部类和“实现"的问题;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 18:39