我们怎么能在密码中做到这一点?
有n个父母p1,p2,p3 ...... pn和m个孩子c1,c2,c3 ...... cm.
假设c1来自(孩子的)p1,p2和p3,并且c2来自(孩子的)p1,p2和p3.
鉴于c1我们能找到c2吗?(来自与c1相同的父母的节点)
孩子可以有1 ... n个父母.
《Effective Java #77》规定,我们必须readResolve在序列化过程中使用单例保证。他们已经使用了这个例子。
public class Elvis implements Serializable{
public static final Elvis INSTANCE = new Elvis();
private Elvis() { ... }
public void leaveTheBuilding() { ... }
Run Code Online (Sandbox Code Playgroud)
他们建议使用
如果Elvis类实现了Serialized,下面的readResolve方法足以保证单例属性:
// readResolve for instance control - you can do better!
private Object readResolve() {
// Return the one true Elvis and let the garbage collector
// take care of the Elvis impersonator.
return INSTANCE; }
Run Code Online (Sandbox Code Playgroud)
此方法忽略反序列化的对象,返回类初始化时创建的杰出 Elvis 实例。