package sun.com.cn.java;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.time.LocalDate;
public class SerialCloneTest {
public static void main(String[] args) throws CloneNotSupportedException{
Employee harry = new Employee("Harry Hacker", 35000, 1989, 10, 1);
Employee harry2 = (Employee) harry.clone();
harry.raiseSalary(10);
System.out.println(harry);
System.out.println(harry2);
}
}
class SerialCloneable implements Cloneable, Serializable
{
public Object clone() throws CloneNotSupportedException
{
try {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
try(ObjectOutputStream out = new ObjectOutputStream(bout))
{
out.writeObject(this);
}
try(InputStream bin = new ByteArrayInputStream(bout.toByteA
使用序列化进行深拷贝
最新推荐文章于 2024-08-04 11:07:52 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)