浅谈Java中实现深拷贝的两种方式—clone() & Serialized
作者:刘知安 时间:2022-04-05 18:36:15
clone() 方法麻烦一些,需要将所有涉及到的类实现声明式接口 Cloneable,并覆盖Object类中的clone()方法,并设置作用域为public(这是为了其他类可以使用到该clone方法)。
序列化的方法简单,需要将所有涉及到的类实现接口Serializable
package b1ch06.clone;
import java.io.Serializable;
class Car implements Cloneable, Serializable {
private String band;
public Car(String band) {
this.band = band;
}
public String getBand() {
return band;
}
public void setBand(String band) {
this.band = band;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
package b1ch06.clone;
import java.io.Serializable;
class Employee implements Cloneable, Serializable {
private String name;
private Car car;
public Employee(String name, Car car) {
this.name = name;
this.car = car;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Car getcar() {
return car;
}
public void setcar(Car car) {
this.car = car;
}
protected void test() {
System.out.println("test func");
}
@Override
public Object clone() throws CloneNotSupportedException {
Employee employee_cloned = (Employee) super.clone();
Car car_cloned = (Car) this.car.clone();
employee_cloned.setcar(car_cloned);
return employee_cloned;
}
}
package b1ch06.clone;
import java.io.*;
public class SerializedClone {
@SuppressWarnings("unchecked")
public static <T extends Serializable> T clone(T obj) {
T cloneObj = null;
try {
//写入字节流
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream obs = new ObjectOutputStream(out);
obs.writeObject(obj);
obs.close();
//分配内存,写入原始对象,生成新对象
ByteArrayInputStream ios = new ByteArrayInputStream(out.toByteArray());
ObjectInputStream ois = new ObjectInputStream(ios);
//返回生成的新对象
cloneObj = (T) ois.readObject();
ois.close();
} catch (Exception e) {
e.printStackTrace();
}
return cloneObj;
}
}
package b1ch06.clone;
public class MyClone {
public static void main(String[] args) {
Car car = new Car("BMW");
Employee employee = new Employee("ANDY", car);
// 方法一:覆盖所有涉及到的类的clone()方法
try {
Employee employee_cp = (Employee) employee.clone();
System.out.println("=========================");
System.out.println("original对象地址?:");
System.out.println(employee.toString());
System.out.println("copy对象地址?:");
System.out.println(employee_cp.toString());
System.out.println("前后两个对象指向同一地址?:");
System.out.println(employee_cp == employee);
System.out.println("=========================");
System.out.println("original对象中car对象地址?:");
System.out.println(employee.getcar().toString());
System.out.println("copy对象中car对象地址?:");
System.out.println(employee_cp.getcar().toString());
System.out.println("前后两个car对象指向同一地址?:");
System.out.println(employee_cp == employee);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
// 方法二:序列化实现深拷贝
Employee cloned_employee = SerializedClone.clone(employee);
System.out.println("=========================");
System.out.println("original对象地址?:");
System.out.println(employee.toString());
System.out.println("copy对象地址?:");
System.out.println(cloned_employee.toString());
System.out.println("前后两个对象指向同一地址?:");
System.out.println(cloned_employee == employee);
System.out.println("=========================");
System.out.println("original对象中car对象地址?:");
System.out.println(employee.getcar().toString());
System.out.println("copy对象中car对象地址?:");
System.out.println(cloned_employee.getcar().toString());
System.out.println("前后两个car对象指向同一地址?:");
System.out.println(cloned_employee == employee);
}
}
以上所述是小编给大家介绍的Java中实现深拷贝的两种方式--——clone() & Serialized详解整合网站的支持!
来源:https://blog.csdn.net/qq_37174526/article/details/86484517
标签:java,clone,深拷贝,Serialized
0
投稿
猜你喜欢
Android 实现左滑出现删除选项
2021-05-28 12:05:59
Java中抽象类和接口介绍
2022-05-28 02:42:09
Android自定义实现BaseAdapter的优化布局
2022-10-29 14:56:59
java利用递归算法实现对文件夹的删除功能
2023-07-26 06:37:02
C#开发之Socket网络编程TCP/IP层次模型、端口及报文等探讨
2023-03-28 14:49:53
Jar包冲突问题原理及解决方案
2023-03-05 09:40:02
android实现状态栏添加图标的函数实例
2022-11-19 06:57:42
UGUI ScrollRect滑动定位优化详解
2021-12-06 09:56:13
c++与c#的时间转换示例分享
2022-08-22 07:21:40
带你了解Java的类和对象
2022-05-08 09:10:21
Java 比较接口comparable与comparator区别解析
2022-11-26 20:54:24
Java设计模式之工厂模式分析【简单工厂、工厂方法、抽象工厂】
2021-07-07 21:11:57
Android中使用GridView实现仿微信图片上传功能(附源代码)
2023-11-27 02:01:25
Springboot如何设置过滤器及重复读取request里的body
2022-05-26 04:59:20
Java SE求解汉诺塔问题的示例代码
2022-05-10 23:44:30
Java中的Static class详解及实例代码
2023-12-24 18:19:40
Java深度复制功能与用法实例分析
2023-07-05 11:23:17
VB.NET中TextBox的智能感知应用实例
2021-07-09 09:36:31
java+opencv实现人脸识别功能
2023-02-20 21:52:58
java使用listIterator逆序arraylist示例分享
2022-06-16 05:33:10