简体   繁体   English

作用域中无法访问类型为OuterClass.StaticNestedClass的封闭实例

[英]No enclosing instance of the type OuterClass.StaticNestedClass is accessible in scope

I am still learning and currently trying to implement DoublyLinkedLists with nested STATIC classes and getting the error below: 我仍在学习,目前正在尝试使用嵌套的STATIC类实现DoublyLinkedLists,并得到以下错误:

No enclosing instance of the type OuterClass.StaticNestedClass is accessible in scope Actual error: No enclosing instance of the type OuterClass.StaticNestedClass is accessible in scope实际错误:
No enclosing instance of the type SolutionDLL.Node is accessible in scope

I have two STATIC nested classes inside the outer class SolutionDLL class: 我在外部类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
        }
    }
}

No matter if i call like this: 无论我是否这样打电话:
SolutionDLL.Node.this.getElement()
of like this: 像这样:
Node.this.getElement()

I still get the error. 我仍然收到错误。 I had the skeleton code given and this is the first time I am implementing with nested classes. 我给出了框架代码,这是我第一次使用嵌套类实现。 So any help would be well appreciated. 因此,任何帮助将不胜感激。 Thanks! 谢谢!

SolutionDLL.Node , just like any class, does not have this field. 就像任何类一样, SolutionDLL.Node没有this字段。 this field is only available inside object and its inner classes of that object class. this字段仅在对象内部以及该对象类的内部类中可用。

Change addFirst to get the values fomr n node instead: 更改addFirst以获得值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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM