gpt4 book ai didi

Java:结合泛型、内部类和 "implements"的问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:21:20 27 4
gpt4 key购买 nike

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

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

这是什么存储什么的布局:

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

我的问题是在声明 LinkedList 时:

LinkedList<HeapNode> heap;

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?

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.

Code:

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

节点.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());
}
}

链表.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>> )

所以两者不兼容。

你需要,在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;
...

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

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

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

现在应该可以编译了。它是否实现了将所有事物与其他事物进行比较的目标就很难说了!

关于Java:结合泛型、内部类和 "implements"的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28653444/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com