简体   繁体   English

Java:结合泛型,内部类和“实现”的问题

[英]Java: Issue combining Generics, Inner class, and “implements”

I am having an issue combining generics, implements , and inner classes. 我遇到了泛型, implements和内部类的问题。 I am creating a LinkedBinaryHeap class which contains an inner class. 我正在创建一个包含内部类的LinkedBinaryHeap类。 This inner class is the generic HeapNode which extends the generic Node class I created; 这个内部类是通用的HeapNode ,它扩展了我创建的通用Node类; it just adds a variable and methods for a key/priority. 它只是为键/优先级添加变量和方法。

In LinkedBinaryHeap I create a generic LinkedList to store HeapNode s. LinkedBinaryHeap我创建了一个通用的LinkedList来存储HeapNode I am assuming the generic data being stored extends Comparable class. 我假设存储的通用数据扩展了Comparable类。

Here is a layout of what stores what: 这是什么存储的布局:

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

My issue is that when declaring the LinkedList : 我的问题是在声明LinkedList

LinkedList<HeapNode> heap;

eclipse underlines HeapNode and gives me the error: eclipse强调HeapNode并给我错误:

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

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? 认为错误告诉我HeapNode必须实现Comparable ,但是我的Node类实现了Comparable ,所以这是照顾的,对吗?

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. 请注意,我已经尝试将implements Comparable Node<T>HeapNode内部类中HeapNode ,并且它什么都没有改变。

Code: 码:

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.
    }
}

As per your definitions: 根据您的定义:

  1. HeapNode is a subtype of Node<E> but implements Comparable<Node<E>> HeapNodeNode<E>的子类型,但implements Comparable<Node<E>>
  2. LinkedList requires a type argument such that T implements Comparable<T> LinkedList需要一个类型参数,以便T implements Comparable<T>
  3. ie a LinkedList<HeapNode> requires that HeapNode implements Comparable<HeapNode> LinkedList<HeapNode>要求HeapNode implements Comparable<HeapNode>
  4. which it does not (from (1), above, it implements Comparable<Node<E>> ) 它没有(从上面的(1),它implements Comparable<Node<E>>

So the two are not compatible. 所以两者不兼容。

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: LinkedList ,您需要将节点类型表示为类型参数,适当地限制,以及节点类型的组件类型参数,也适当地限制:

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;
  ...

Now your LinkedBinaryHeap needs to adjust it's use of LinkedList : 现在你的LinkedBinaryHeap需要调整它对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! 它是否实现了将所有内容与其他所有内容进行比较的目标更难说!

LinkedList requires that T implements Comparable. LinkedList要求T实现Comparable。 HeapNode implements Comparable<Node<E>> . HeapNode实现Comparable<Node<E>> HeapNode != Node so it doesn't satisfy the type bound. HeapNode!= Node因此它不满足类型绑定。 Change LinkedList's declaration to T extends Comparable<? super T> 将LinkedList的声明更改为T extends Comparable<? super T> T extends Comparable<? super T> . T extends Comparable<? super T> This is fine, type-wise: HeapNode is declaring that it can compare itself with any Node or subtype of Node. 这是好的,类型:HeapNode声明它可以与Node的任何Node或子类型进行比较。 Of course, this means that HeapNode needs to override compareTo , but you're kind of stuck there, what with Node already implementing Comparable. 当然,这意味着HeapNode需要覆盖compareTo ,但是你有点卡在那里,Node已经实现了Comparable。

Edit: as comments point out, this answer is wrong, and not worth fixing because there's a right answer. 编辑:正如评论指出的那样,这个答案是错误的,不值得修复,因为有正确的答案。 Let it stay for posterity as a lesson in...something. 让它留给后人作为......的一课。 How I'm not as knowledgeable on generics as I'd like to be, maybe. 也许,我不像我想要的那样了解仿制药。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM