队列实现构造函数错误JAVA
有人能告诉我为什么我会说 隐式超级构造函数节点()未定义。必须明确 调用另一个构造函数 我知道,在某些情况下,当Java编译器版本与代码混合时,eclipse会出现此错误,但我的情况并非如此。这是我的代码,错误在Queue2构造函数的Queue2类中队列实现构造函数错误JAVA,java,eclipse,oop,queue,nodes,Java,Eclipse,Oop,Queue,Nodes,有人能告诉我为什么我会说 隐式超级构造函数节点()未定义。必须明确 调用另一个构造函数 我知道,在某些情况下,当Java编译器版本与代码混合时,eclipse会出现此错误,但我的情况并非如此。这是我的代码,错误在Queue2构造函数的Queue2类中 import java.util.NoSuchElementException; public class Queue2<T, Item> extends Node<T> { private Node<T
import java.util.NoSuchElementException;
public class Queue2<T, Item> extends Node<T> {
private Node<T> head;
private Node<T> tail;
// good practice to initialize all variables!
public Queue2() {
head = null;
tail = null;
}
public void enqueue(T newData) {
// make a new node housing newData
Node<T> newNode = new Node<T>(newData);
// point _head to newNode if queue is empty
if (this.isEmpty()) {
_head = newNode;
}
// otherwise, set the current tail’s next
// pointer to the newNode
else {
_tail.setNext(newNode);
}
// and make _tail point to the newNode
_tail = newNode;
}
// in class Queue<Type> …
public Type dequeue() {
if (this.isEmpty()) {
return null;
}
// get _head’s data
Type returnData = _head.getData();
// let _head point to its next node
_head = _head.getNext();
// set _tail to null if we’ve dequeued the
// last node
if (_head == null){
_tail = null;
}
return returnData;
public boolean isEmpty() {
// our Queue is empty if _head
// is pointing to null!
return _head == null;
}
}
import java.util.NoSuchElementException;
公共类队列2扩展节点{
专用节点头;
私有节点尾部;
//初始化所有变量的良好实践!
公共队列2(){
head=null;
tail=null;
}
公共void排队(T newData){
//创建包含新数据的新节点
Node newNode=新节点(newData);
//如果队列为空,则指向新节点
if(this.isEmpty()){
_头=新节点;
}
//否则,设置当前尾部的下一个
//指向新节点的指针
否则{
_tail.setNext(newNode);
}
//并将尾端指向新节点
_tail=newNode;
}
//在类队列中…
公共类型出列(){
if(this.isEmpty()){
返回null;
}
//获取头的数据
键入returnData=_head.getData();
//让_head指向它的下一个节点
_head=_head.getNext();
//如果我们已将
//最后节点
如果(_head==null){
_tail=null;
}
返回数据;
公共布尔值为空(){
//我们的队伍是空的,如果
//正在指向null!
返回_head==null;
}
}
这是超级类…我意识到getter和setter并不完整,但我相信这与我的错误无关
public class Node<Type> {
private Type _data;
private Node<Type> _nextNode;
public Node(Type newData) {
_data = newData;
_nextNode = null;
}
public void setNext(Node<T> newNextNode){
}
public Node<Type> getNext() {
}
public Type getData() {
}
public void setData(Node<T> newData){
}
}
公共类节点{
私有类型_数据;
私有节点_nextNode;
公共节点(newData类型){
_数据=新数据;
_nextNode=null;
}
public void setNext(Node newNextNode){
}
公共节点getNext(){
}
公共类型getData(){
}
公共void setData(节点newData){
}
}
顺便说一句,这只是一些代码做一些队列练习!
提前感谢大家!!!因为您在
节点类中没有默认的非私有构造函数。您必须在节点类中定义默认构造函数,或者从队列类中调用另一个节点类的构造函数
或者避免这种疯狂的解决方案,因为队列可能会保留节点对象,而不是从节点类派生问题在于,您有队列2扩展节点
,您没有节点
的无参数构造函数,队列2
的构造函数没有指示哪个节点
构造或者应该被叫来
我认为您实际上不希望Queue2
成为节点
的子类(您有,但没有),因此请更改以下内容:
public class Queue2<T, Item> extends Node<T> {
公共类队列2扩展节点{
为此:
public class Queue2<T, Item> {
公共类队列2{
我怀疑节点的唯一构造函数是这样的:
public Node<T>(T value)
含蓄地说:
public Queue2() {
super(); // This is what's failing.
head = null;
tail = null;
}
对于Queue2
来说,首先扩展Node
意义不大。只需使用组合而不是继承。为什么要将队列视为节点?队列的节点值是多少?上一个节点是什么?下一个节点是什么?当您创建一个类而不定义构造函数时,Java为您定义一个隐式的,没有参数的(并且不做任何事情)
如果发生这种情况,但您从另一个类继承(如从节点继承的Queue2),此隐式构造函数也将调用父类构造函数,因此等效于:
public Queue2() {
super();
}
您看到的错误与父类没有默认(无参数)构造函数有关,因此此“隐式”代码无法工作
若要解决此问题,请自己定义一个构造函数,传递节点构造函数所需的任何参数。请参见此上的。我们可以查看节点类吗?…或者首先避免这种疯狂的继承。
public Queue2() {
super();
}