Reference
题目:
Implement the class ReferenceManager. Include the following two methods:
copyValue(Node obj). This would just copy the value from parameter obj to the public attribute node. But obj and node are still two difference instances / objects.
copyReference(Node obj). This would copy the reference from obj to node. So that both node and obj are point to the same object.
分析:
这是个可以帮助理解deep copy的好例子。
解法:
public class ReferenceManager {
public Node node;
public void copyValue(Node obj) {
// copy value from obj to node
node = new Node(obj.val);
}
public void copyReference(Node obj) {
// copy reference from obj to node
node = obj;
}
}