為什么需要克隆機(jī)制?
克隆是將對(duì)象的值拿過來遵岩,方便快速copy一個(gè)對(duì)象,java中有兩種克隆方式,一個(gè)是淺度克隆,另外一個(gè)則是深度克隆(deep copy),有對(duì)比就有區(qū)別。
他們之間有什么區(qū)別?
淺度克隆是克隆對(duì)象的副本,克隆對(duì)象的引用武福,在JAVA中的實(shí)現(xiàn)方式是只需要實(shí)現(xiàn)標(biāo)記接口Cloneable即可(標(biāo)記該對(duì)象可以調(diào)用Object.clone()方法)
A class implements the Cloneable interface to indicate to the Object.clone() method that it is legal for that method to make a field-for-field copy of instances of that class.
淺度克隆代碼示例:
import java.io.*;
import java.util.Objects;
import java.util.Objects;
/**
* Created by Administrator on 2019/6/12.
*/
public class ShallowCloneObject implements Cloneable {
private String name;
private int age;
private Object object;
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 Object getObject() {
return object;
}
public void setObject(Object object) {
this.object = object;
}
public static void main(String[] args) throws CloneNotSupportedException {
//源對(duì)象
ShallowCloneObject source=new ShallowCloneObject();
source.setAge(18);
source.setName("淺度克隆");
source.setObject(new Object());
System.out.println(source.equals(source.clone()));
//克隆出來的新對(duì)象
ShallowCloneObject target= (ShallowCloneObject) source.clone();
//克隆的對(duì)象屬性是相等的,打印出true
assert target.getName()==source.getName();
System.out.println(target.getName()==source.getName());
assert target.getObject()==source.getObject();
System.out.println(target.getObject()==source.getObject());
}
}
深度克隆是指克隆對(duì)象的值榕茧,相當(dāng)于值傳遞,實(shí)現(xiàn)方式是對(duì)象字節(jié)流進(jìn)行序列化與反序列化轉(zhuǎn)換式傳輸,深度克隆出來的對(duì)象里面包含的屬性指向的是新的堆空間(注意的是如果用到對(duì)象字節(jié)流的話所有的對(duì)象屬性包括自己都必須要實(shí)現(xiàn)序列化,基本類型除外)
深度克隆代碼示例
import java.io.*;
import java.lang.reflect.Field;
import java.util.Objects;
/**
* Created by Administrator on 2019/6/12.
*深度克隆對(duì)象示例
*/
public class DeepCloneObject implements Serializable{
private String name;
private int age;
private SeriaObject object;
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 Object getObject() {
return object;
}
public void setObject(SeriaObject object) {
this.object = object;
}
public DeepCloneObject deepClone() throws IOException, ClassNotFoundException {
Field[] fields= this.getClass().getDeclaredFields();
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<Void>() {
public Void run() {
for(Field fs:fields) {
fs.setAccessible(true);
}
return null;
}
});
//檢測(cè)所有的屬性是否是實(shí)現(xiàn)了序列化接口
for(Field f:fields){
if(f.getClass().isPrimitive()){
continue;
}
if(f.getClass().isInstance(Serializable.class)){
throw new RuntimeException("the field:["+f.getName()+"] must be implements Serializable interface exlude primitive class");
}
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(this);
//將當(dāng)前這個(gè)對(duì)象寫到一個(gè)輸出流當(dāng)中咐蝇,,因?yàn)檫@個(gè)對(duì)象的類實(shí)現(xiàn)了Serializable這個(gè)接口同木,所以在這個(gè)類中
//有一個(gè)引用,這個(gè)引用如果實(shí)現(xiàn)了序列化跛十,那么這個(gè)也會(huì)寫到這個(gè)輸出流當(dāng)中
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
return (DeepCloneObject) ois.readObject();
}
@Override
public String toString() {
return "DeepCloneObject{" +
"name=" + name +
", age=" + age +
'}';
}
static class SeriaObject extends Object implements Serializable{}
public static void main(String[] args) throws CloneNotSupportedException, IOException, ClassNotFoundException {
//源對(duì)象
DeepCloneObject source=new DeepCloneObject();
source.setAge(18);
source.setName("深度克隆");
source.setObject(new SeriaObject());
//克隆出來的新對(duì)象
DeepCloneObject target=source.deepClone();
//克隆的對(duì)象屬性是相等的,打印出false
System.out.println(target.getName()==source.getName());
System.out.println(target.getObject()==source.getObject());
}
}
·