名为XYZ的类包含自己作为类成员

时间:2018-08-19 06:12:43

标签: java class object

我已经开始学习java,并取得了一些进步。 但是,在实现链接列表时,我不理解下面的代码

Class Node
{
    Node next;
    int i;
}

在上面的代码中,我创建了一个名为 Node 的类,并创建了 Node 类型的变量。如果我打印该下一个班级成员,则它将NULL

我不明白这个概念。

如果“下一个”保留了变量“节点”的引用,为什么不在屏幕上打印其地址,为什么打印NULL值呢?

1 个答案:

答案 0 :(得分:0)

您需要执行以下操作:

Node node = new Node(); // creating first node 
node.i = 0;
Node node1 = new Node(); // creating second node
node.i = 1;
node.next = node1; // assigning next node to previous node
//Now print here for node one
System.out.println("Next node: "+node.next); // it will print identity hashcode not the address as per default toString implementation.

需要进行分配,分配本身不会发生。 //拇指法则