做项目过程中有需求用到对象复制内容, 在此随手记录一下.
为何要复制对象, 在此就不做细数, 下面简单说明文字+代码.
对象复制, 总体来将, 分为三种方法.
方法一:
用getter和setter挨个进行赋值, 这种方式简单, 但实际用起来比较麻烦.. 也就是当类比较简单的时候还可以, 类比较复杂的时候, 就不适用了 .在此就不做介绍了.
方法二:
要复制的类实现接口(java.lang.Cloneable), 并且重写clone()方法.
public class PersonBean implements Cloneable{
@Override
public Object clone(){
PersonBean bean = null;
try{
bean = (PersonBean) super.clone();
}catch (CloneNotSupportedException e){
e.printStackTrace();
}
return bean;
}
}
当类中参数为引用类型的时候, 此引用类型的类还是需要实现接口Cloneable, 并且还需要在当前类clone()方法中调用引用对象的clone()方法进行赋值. 在项目中实际操作的时候, 可能会需要些N多次实现Cloneable接口, 重新clone()方法, 赋值等操作, 比较麻烦.
方法三:
将对象序列化(Serializable), 写到内存中, 然后再读出. 这种方法个人觉得比较通用, 随手写了一下.
代码如下:
package com.example.chanp.testannotation.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
* Created by sjx on 2019/6/27.
*/
public class CloneObjectUtils {
public static <T> T cloneObject(T obj) {
T result = null;
ByteArrayOutputStream byteArrayOutputStream = null;
ByteArrayInputStream byteArrayInputStream = null;
ObjectOutputStream outputStream = null;
ObjectInputStream inputStream = null;
try {
//对象写到内存中
byteArrayOutputStream = new ByteArrayOutputStream();
outputStream = new ObjectOutputStream(byteArrayOutputStream);
outputStream.writeObject(obj);
//从内存中再读出来
byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
inputStream = new ObjectInputStream(byteArrayInputStream);
result = (T) inputStream.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (outputStream != null)
outputStream.close();
if (inputStream != null)
inputStream.close();
if (byteArrayOutputStream != null)
byteArrayOutputStream.close();
if (byteArrayInputStream != null)
byteArrayInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
}
实体类: PersonBean
/**
* Created by chanp on 2018/7/4.
*/
public class PersonBean implements Serializable {
private int id;
private String name;
private int age;
private long time;
private AddressBean addressBean;
public PersonBean() {
}
public PersonBean(int id, String name, int age, long time) {
this.id = id;
this.name = name;
this.age = age;
this.time = time;
}
public PersonBean(int id, String name, int age, long time, AddressBean addressBean) {
this.id = id;
this.name = name;
this.age = age;
this.time = time;
this.addressBean = addressBean;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
public AddressBean getAddressBean() {
return addressBean;
}
public void setAddressBean(AddressBean addressBean) {
this.addressBean = addressBean;
}
@Override
public String toString() {
return "PersonBean{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", time=" + time +
", addressBean=" + addressBean +
'}';
}
}
实体类: AddressBean
/**
* Created by chanp on 2019/6/27.
*/
public class AddressBean implements Serializable {
private String address;
private String code;
private List<String> list;
public AddressBean(String address, String code) {
this.address = address;
this.code = code;
}
public AddressBean(String address, String code, List<String> list) {
this.address = address;
this.code = code;
this.list = list;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
@Override
public String toString() {
return "AddressBean{" +
"address='" + address + '\'' +
", code='" + code + '\'' +
", list=" + list +
'}';
}
}
调用CloneObjectUtils方法:
PersonBean personBean = new PersonBean(1, "name1", 11, 111);
AddressBean addressBean = new AddressBean("北京市", "0987");
personBean.setAddressBean(addressBean);
List<String> list = new ArrayList<>();
addressBean.setList(list);
list.add("array1");
list.add("array2");
Log.i("Test", "1_result toString : " + personBean.toString());
PersonBean result = CloneObjectUtils.cloneObject(personBean);
Log.i("Test", "2_result toString : " + result.toString());
result.setName("王八犊子");
result.getAddressBean().setAddress("海南省");
result.getAddressBean().getList().remove(0);
Log.i("Test", "1_result toString : " + personBean.toString());
Log.i("Test", "3_result toString : " + result.toString());
输出结果:
如有问题欢迎留言, 共同讨论.