以下是基类相关的代码DblyLinkListDblyLinkListhere的完整代码,

package JavaCollections.list;

public class DblyLinkList<T> implements Iterable<T>{

   protected DListNode<T> sentinel;
   protected int size;

   class DListNode<T> {
       private T item;
       private DListNode<T> prev;
       private DListNode<T> next;

       DListNode(T item, DListNode<T> p, DListNode<T> n) {
           this.item = item;
           this.prev = p;
           this.next = n;
        }
    }

    DListNode<T> newNode(T item, DListNode<T> prev, DListNode<T> next) {
       return new DListNode(item, prev, next);
    }

    public DblyLinkList() {
       this.sentinel = this.newNode(null, this.sentinel, this.sentinel);
    }

    .........
}


下面是在同一个程序包中引入的类LockableList,该类在每次创建节点时都将lock字段设置为false。要删除LockableNode节点,remove()方法将检查lock字段的值。

调用super(item, p, n);时,错误:The constructor DblyLinkList<T>.DListNode<T>(T, DblyLinkList<T>.DListNode<T>, DblyLinkList<T>.DListNode<T>) is undefined

如何在下面的派生类LockableList.LockableNode中编写可被重写方法newNode调用的构造函数?

package JavaCollections.list;

import JavaCollections.list.DblyLinkList.DListNode;

public class LockableList<T> extends DblyLinkList<T> {

    class LockableNode<T> extends DListNode<T>{
        /**
         * Do not lock the node during creation of a node.
         */
        private boolean lock;

        LockableNode(T item, DblyLinkList<T>.DListNode<T> p,
                        DblyLinkList<T>.DListNode<T> n) {
            super(item, p, n); //this does not work
            this.lock = false;
        }
    }

    @Override
    LockableNode<T> newNode(T item, DListNode<T> prev, DListNode<T> next) {
        //return new LockableNode(item, prev, next); //How do i call the constructor?
    }

    public LockableList(){
        this.sentinel = this.newNode(null, this.sentinel, this.sentinel);
    }

    ........
}


注意:这是继承和封装的练习

最佳答案

尝试这个:

LockableNode(T item, DListNode<T> p,
            DListNode<T> n) {
        super(item, p, n);
        this.lock = false;
}

07-28 13:12