java继承内部类,如何在这段代码中继承父类的内部类?

Below is the parent class DblyLinkList

package JavaCollections.list;

import java.util.Iterator;

import java.util.NoSuchElementException;

public class DblyLinkList implements Iterable{

class DListNode {

private T item;

private DListNode prev;

private DListNode next;

DListNode(T item, DListNode p, DListNode n) {

this.item = item;

this.prev = p;

this.next = n;

}

}

.....

}

Below is the derived class LockableList,

package JavaCollections.list;

import JavaCollections.list.DblyLinkList.DListNode;

public class LockableList extends DblyLinkList {

class LockableNode extends DblyLinkList.DListNode {

/**

* lock the node during creation of a node.

*/

private boolean lock;

LockableNode(T item, DblyLinkList.DListNode p,

DblyLinkList.DListNode n) {

super(item, p, n); // this does not work

this.lock = false;

}

}

LockableNode newNode(T item, DListNode prev, DListNode next) {

return new LockableNode(item, prev, next);

}

public LockableList() {

this.sentinel = this.newNode(null, this.sentinel, this.sentinel);

}

.....

}

If class LockableNode extends DListNode in the above code, error:The constructor DblyLinkList.DListNode(T, DblyLinkList.DListNode, DblyLinkList.DListNode) is undefined occurs at line super(item, p, n)

This error is resolved by saying: class LockableNode extends DblyLinkList.DListNode

How do I understand this error? Why it got resolved?

解决方案

You are redeclaring the type variable T in the inner class. That means that within the inner class, the T of the outer class is hidden and cannot be referred to anymore.

Since you have a non-static inner class, you can just remove the type variable T there:

class DListNode { ... }

because it inherits it from the containing class (and probably you mean that the variables are the same, anyway).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值