我写了一个代码 -
// Node Class
class aNode {
// Node Contents
int NodeInt;
char NodeChar;
// constructor
aNode() {
}
aNode(int x, char y) {
NodeInt = x;
NodeChar = y;
}
}
class MainClass {
static aNode node = new aNode();
public static void main(String[] args) {
node = null;
function(node);
if (node == null) {
System.out.println("Node is null");
}
}
static void function(aNode x) {
if (x == null) {
System.out.println("Node is null");
}
x = new aNode(5, 'c');
System.out.println(x.NodeInt);
System.out.println(x.NodeChar);
}
}
我预计输出是 -
Node is null
5
c
但是当程序返回到 main 时,node 的值又被设置为 null。所以我得到的输出是 -
Node is null
5
c
Node is null
请帮我修改代码以获得所需的输出。任何帮助将不胜感激!