******************************************正确写法******************************************************
package com.example.SpringBootTest1.shejimoshi.singleton;
import java.io.ObjectStreamException;
import java.io.Serializable;
public class InnerclassSingleton implements Serializable {
public static final long serialVersionUID = 42L;
private InnerclassSingleton() {
if (InnerclassSingleton.getInstance() != null) {
throw new RuntimeException("单例模式不允许创建多个实例");
}
};
private static class Singleton {
private static final InnerclassSingleton i = new InnerclassSingleton();
}
public static InnerclassSingleton getInstance() {
return Singleton.i;
}
Object readResolve() throws ObjectStreamException {
return InnerclassSingleton.getInstance();
}
}
package com.example.SpringBootTest1.shejimoshi.singleton;
import java.io.*;
public class Test {
public static void main(String[] args) throws IOException, ClassNotFoundException {
InnerclassSingleton instance = InnerclassSingleton.getInstance();
// 序列化
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("testSerializable"));
oos.writeObject(instance);
oos.close();
// 反序列化
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("testSerializable"));
InnerclassSingleton instance2 = (InnerclassSingleton) ois.readObject();
System.out.println(instance == instance2); // true
}
}
package com.example.SpringBootTest1.shejimoshi.singleton;
import java.io.ObjectStreamException;
import java.io.Serializable;
public class InnerclassSingleton implements Serializable {
public static final long serialVersionUID = 42L;
private InnerclassSingleton() {
if (InnerclassSingleton.getInstance() != null) {
throw new RuntimeException("单例模式不允许创建多个实例");
}
};
private static class Singleton {
private static final InnerclassSingleton i = new InnerclassSingleton();
}
public static InnerclassSingleton getInstance() {
return Singleton.i;
}
Object readResolve() throws ObjectStreamException {
return InnerclassSingleton.getInstance();
}
int i = 0; // 新增一行代码
}
package com.example.SpringBootTest1.shejimoshi.singleton;
import java.io.*;
public class Test {
public static void main(String[] args) throws IOException, ClassNotFoundException {
InnerclassSingleton instance = InnerclassSingleton.getInstance();
// 序列化
// ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("testSerializable"));
// oos.writeObject(instance);
// oos.close();
// 反序列化
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("testSerializable"));
InnerclassSingleton instance2 = (InnerclassSingleton) ois.readObject();
System.out.println(instance == instance2); // true (新增了一行代码后打印还是true)
}
}
****************************************非正常写法**************************************
public class InnerclassSingleton implements Serializable {
// public static final long serialVersionUID = 42L;
private InnerclassSingleton() {
if (InnerclassSingleton.getInstance() != null) {
throw new RuntimeException("单例模式不允许创建多个实例");
}
};
private static class Singleton {
private static final InnerclassSingleton i = new InnerclassSingleton();
}
public static InnerclassSingleton getInstance() {
return Singleton.i;
}
Object readResolve() throws ObjectStreamException {
return InnerclassSingleton.getInstance();
}
}
package com.example.SpringBootTest1.shejimoshi.singleton;
import java.io.*;
public class Test {
public static void main(String[] args) throws IOException, ClassNotFoundException {
InnerclassSingleton instance = InnerclassSingleton.getInstance();
// 序列化
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("testSerializable"));
oos.writeObject(instance);
oos.close();
// 反序列化
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("testSerializable"));
InnerclassSingleton instance2 = (InnerclassSingleton) ois.readObject();
System.out.println(instance == instance2); // true
}
}
public class InnerclassSingleton implements Serializable {
// public static final long serialVersionUID = 42L;
private InnerclassSingleton() {
if (InnerclassSingleton.getInstance() != null) {
throw new RuntimeException("单例模式不允许创建多个实例");
}
};
private static class Singleton {
private static final InnerclassSingleton i = new InnerclassSingleton();
}
public static InnerclassSingleton getInstance() {
return Singleton.i;
}
Object readResolve() throws ObjectStreamException {
return InnerclassSingleton.getInstance();
}
int i = 0; // 新增一行代码
}
package com.example.SpringBootTest1.shejimoshi.singleton;
import java.io.*;
public class Test {
public static void main(String[] args) throws IOException, ClassNotFoundException {
InnerclassSingleton instance = InnerclassSingleton.getInstance();
// 序列化
// ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("testSerializable"));
// oos.writeObject(instance);
// oos.close();
// 反序列化
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("testSerializable"));
InnerclassSingleton instance2 = (InnerclassSingleton) ois.readObject();
System.out.println(instance == instance2); // false (由于新增了一行代码,会打印false)
}
}
总结: 如果需要进行网络传输, 需要实现序列化接口, 并且指定一个序列化编号(反序列化时, 尽管类代码有变动, 还是会返回指定编号的对象)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix