【问题标题】:Queue implementation constructor error JAVA [duplicate]队列实现构造函数错误JAVA [重复]
【发布时间】:2013-02-23 01:02:51
【问题描述】:

谁能告诉我为什么我会收到错误提示

隐式超级构造函数 Node() 未定义。必须明确 调用另一个构造函数

我知道在某些情况下,当 Java 编译器版本与代码混合时,eclipse 会出现此错误,但对我而言并非如此。这是我的代码,错误在 Queue2 构造函数的 Queue2 类中。

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

    }  

这是超级类...我意识到 getter 和 setter 不完整,但我相信这与我的错误无关? :S

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){

    }
}

顺便说一句,这只是一些用于队列练习的代码! 先谢谢大家了!!!

【问题讨论】:

  • 我们可以看看节点类吗?

标签: java eclipse oop queue nodes


【解决方案1】:

怀疑Node&lt;T&gt;的唯一构造函数是这样的:

public Node<T>(T value)

这完全有道理——一个节点应该有一个值。然后你的 Queue2 构造函数失败,因为:

public Queue2() {
    head = null;
    tail = null;
}

是隐含的:

public Queue2() {
    super(); // This is what's failing.
    head = null;
    tail = null;
}

Queue2 首先扩展Node意义不大。只需使用组合而不是继承。为什么要将队列视为节点?队列的节点值是多少?上一个节点是什么?下一个节点是什么?

【讨论】:

    【解决方案2】:

    问题是你有Queue2&lt;T, Item&gt; extends Node&lt;T&gt;,你没有有一个Node&lt;T&gt; 的无参数构造函数,而且Queue2&lt;T, item&gt; 的构造函数没有指明哪个@应该调用987654326@构造函数。

    我认为你实际上不希望 Queue2&lt;T, Item&gt; 成为 Node&lt;T&gt; 的子类(你有 a has-a relationship,而不是 an is-a relationship),所以改变这个:

    public class Queue2<T, Item> extends Node<T> {
    

    到这里:

    public class Queue2<T, Item> {
    

    【讨论】:

      【解决方案3】:

      当您创建一个类但不定义构造函数时,Java 会为您定义一个隐含的,没有参数(并且什么都不做)。

      当这种情况发生但你从另一个类继承时(比如继承自 Node 的 Queue2),这个隐式构造函数也会调用父类构造函数,所以相当于:

      public Queue2() {
        super();
      }
      

      您看到的错误与父类没有默认(无参数)构造函数有关,因此这个“隐式”代码无法工作。

      要解决这个问题,请自己定义一个构造函数,传递 Node 构造函数所需的任何参数。请参阅official doc

      【讨论】:

        【解决方案4】:

        因为您在Node class 中没有默认的非私有构造函数。您必须在Node class 中定义默认构造函数,或者从您的Queue class 调用Node class 的另一个构造函数

        或者避免这种疯狂的解决方案,因为 Queue 可能会保留 Node 对象,而不是派生自 Node class

        【讨论】:

        • ...或者首先避免这种疯狂的继承。
        猜你喜欢
        • 1970-01-01
        • 2018-08-30
        • 2013-08-23
        • 1970-01-01
        • 2014-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多