简体   繁体   English

使用节点数组的这棵$ 10树

[英]this$10 Tree using Node Array

I'm trying to implement a tree using a nested Node, which itself has a Node array. 我正在尝试使用嵌套Node来实现树,嵌套Node本身具有Node数组。 (see the code bellow) When I instantiate it and debug it I can see a reference inside the start Node that is called this$0, it basically seems to be the same reference as the instantiated Tree itself. (请参见下面的代码)当我实例化它并对其进行调试时,我可以在开始节点内看到一个名为this $ 0的引用,它基本上与实例化的Tree本身是相同的引用。 I was wondering if anyone could tell me why it is there and what purpose it serves (if it is not due to some mistake in the code). 我想知道是否有人可以告诉我它为什么在那里以及它有什么作用(如果不是由于代码中的某些错误而引起的)。 Thanks. 谢谢。

public class NodeTree {
    private Node start;
    private int degree;

    public NodeTree() {
        start = new Node();
    }

    private class Node {
        private Object root;
        private Node[] subtrees;
        private int size;

        Node() {  }      
    }
}

When you see this$_something_ in a debugger, it means that your class has a reference to the object of an outer class. 当您在调试器中看到this$_something_时,表示您的类具有对外部类对象的引用。 This reference is created automatically by the compiler. 此引用由编译器自动创建。

In your code this happens because Node is a non-static class that is nested inside the NodeTree class. 在您的代码中,发生这种情况是因为Node是嵌套在NodeTree类中的非静态类。 This means that it gets a reference to its outer object, ie the NodeTree set automatically. 这意味着它将获取对其外部对象的引用,即自动设置的NodeTree

If you do not want this behavior, make Node static in the NodeTree , or move it out to make it a top-level class: 如果您不希望出现这种情况,请在NodeTree Node静态,或将其移出以使其成为顶级类:

public class NodeTree {
    private Node start;
    private int degree;

    public NodeTree() {
        start = new Node();
    }
    static private class Node {
//  ^^^^^^
        private Object root;
        private Node[] subtrees;
        private int size;
        Node() {  }
    }
}

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

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