在范围内无法访问 OuterClass.StaticNestedClass 类型的封闭实例
No enclosing instance of the type OuterClass.StaticNestedClass is accessible in scope
我仍在学习,目前正在尝试使用嵌套的 STATIC classes 实现 DoublyLinkedLists 并收到以下错误:
No enclosing instance of the type OuterClass.StaticNestedClass is accessible in scope
实际错误:
No enclosing instance of the type SolutionDLL.Node is accessible in scope
我有两个 STATIC 嵌套在 class 里面 class SolutionDLL
class:
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()
我仍然得到错误。我已经给出了框架代码,这是我第一次使用嵌套的 classes 来实现。因此,我们将不胜感激任何帮助。
谢谢!
SolutionDLL.Node
,就像任何 class 一样,没有 this
字段。 this
字段仅在对象内部可用,并且该对象的内部 classes class.
更改 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
}
}
我仍在学习,目前正在尝试使用嵌套的 STATIC classes 实现 DoublyLinkedLists 并收到以下错误:
No enclosing instance of the type OuterClass.StaticNestedClass is accessible in scope
实际错误:
No enclosing instance of the type SolutionDLL.Node is accessible in scope
我有两个 STATIC 嵌套在 class 里面 class SolutionDLL
class:
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()
我仍然得到错误。我已经给出了框架代码,这是我第一次使用嵌套的 classes 来实现。因此,我们将不胜感激任何帮助。 谢谢!
SolutionDLL.Node
,就像任何 class 一样,没有 this
字段。 this
字段仅在对象内部可用,并且该对象的内部 classes class.
更改 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
}
}