Java中getNode方法返回内存地址而非预期Node对象,求技术协助
Hey there, I know it's frustrating when you expect to see your Node's details but get that weird @xxxxxxx string instead—let's fix this quickly!
The Root Cause
In Java, every class inherits from the Object class by default. The default toString() method (which gets called automatically when you concatenate an object to a string, like in your System.out.println call) returns:
getClass().getName() + "@" + Integer.toHexString(hashCode())
That's exactly the "memory address" format you're seeing—it's not actually a raw memory address, but a unique identifier for the object instance using its class name and hash code.
The Fix: Override toString() in Your Node/AcNode Class
To make your System.out.println output the actual properties of your Node, you need to override the toString() method in either the parent Node class or the AcNode subclass. Here's how to do it:
Option 1: Override in the Node Class
Add this method to your Node definition:
@Override public String toString() { return "Node{title='" + title + "', disX=" + disX + ", disY=" + disY + "}"; }
Option 2: Override in the AcNode Subclass (for AcNode-specific output)
If you want to distinguish AcNode instances from regular Node instances in the output, add this to your AcNode class instead:
@Override public String toString() { return "AcNode{title='" + title + "', disX=" + disX + ", disY=" + disY + "}"; }
What Happens Next?
After adding this method, when you run your existing code:
AcNode aNode = new AcNode("Test", 0.5, 0.6); System.out.println("See next node" + aNode.getNode());
You'll get output like this (depending on which toString() you used):
See next nodeNode{title='Test', disX=0.5, disY=0.6}
Or for AcNode's version:
See next nodeAcNode{title='Test', disX=0.5, disY=0.6}
That's exactly the readable object details you were expecting!
内容的提问来源于stack exchange,提问作者czedarts

