java - 在范围内无法访问 OuterClass.StaticNestedClass 类型的封闭实例

标签 java class inner-classes

我仍在学习,目前正在尝试使用嵌套的 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/

相关文章:

Java在命令行中编译代码时出现问题: Package does not exist. ..但确实如此

java - 从 httprequest 创建 JSON,然后解析它

c++ - 如何在 C++ 中编写默认构造函数?

Java 构造函数调用被忽略/没有抛出错误

java - 二维阵列打印在一列错误

javascript - 如何使用 Jasmine 监视静态类方法

c++ - 在派生类 C++ 中将基类的 protected 成员访问声明为 public

java - 从私有(private)类访问公共(public)类中的方法/字段

java - 在 Java 中,嵌套类与其外部类之间的关系是什么?

c++ - 您将如何从嵌套类的方法中获取基类的 "this"指针?