我仍在学习,目前正在尝试使用嵌套的 STATIC 类实现 DoublyLinkedLists 并收到以下错误:
在范围内无法访问 OuterClass.StaticNestedClass 类型的封闭实例
实际错误:
在范围内无法访问 SolutionDLL.Node 类型的封闭实例
我在外部类 SolutionDLL
类中有两个 STATIC 嵌套类:
class SolutionDLL {
public static class Node {
private Object element;
private Node next;
private Node previous;
Node(Object elementIn, Node nextNodeIn, Node prevNodeIn) {
element = elementIn;
next = nextNodeIn;
previous = prevNodeIn;
}
public Object getElement() {
return element;
}
public Node getNext() {
return next;
}
public Node getPrevious() {
return previous;
}
}
public static class DLList {
public void addFirst(Node n) {
SolutionDLL.Node tempNode = new SolutionDLL.Node(
SolutionDLL.Node.this.getElement(),
SolutionDLL.Node.this.getNext(),
SolutionDLL.Node.this.getPrevious());
// do something
}
}
}
无论我这样打电话:
SolutionDLL.Node.this.getElement()
像这样:
Node.this.getElement()
我仍然收到错误。我给出了框架代码,这是我第一次使用嵌套类实现。因此,任何帮助将不胜感激。 谢谢!
最佳答案
SolutionDLL.Node
与任何类一样,没有 this
字段。 this
字段仅在对象及其该对象类的内部类内部可用。
更改 addFirst
以获取 n
节点的值:
public static class DLList {
private Node firstNode = null;
public void addFirst(Node n) {
//do something with the first node before it is assigned to the n
if (firstNode != null){
SolutionDLL.Node tempNode = new SolutionDLL.Node(
firstNode.getElement(),
firstNode.getNext(),
firstNode.getPrevious());
}
firstNode = n;
// do something
}
}
关于java - 在范围内无法访问 OuterClass.StaticNestedClass 类型的封闭实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47601152/