发表于 2012-10-12 02:03:48 by 月小升
浅层复制的时候,一个对象仅仅在引用另外一个对象。属性没被clone。而深层复制将属性也一并clone.
Java对象的浅层复制是指Java对象A本身被clone成新对象B,但A的属性没有被clone处理,只是把A的各个属性所指的对象赋值到B对应的属性上,A与B的相同属性都引用到同一个对象。
Java对象的深层复制是指Java对象A本身被clone成新对象B,同时A的属性也是被clone成新对象,赋值到A的各个属性上去,A与B的相同属性都引用到不同的对象;
先看一个浅层复制基本的例子
package com.javaer.examples;
import java.util.Date;
/**
* java浅层复制的特点
*
* @author mc2
*
*/
public class ShallowCopy {
private String name;
private int age;
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;
}
/**
* @param args
*/
public static void main(String[] args) {
ShallowCopy a = new ShallowCopy();
a.setName("yuexiaosheng");
a.setAge(30);
System.out.println("a name:" + a.getName());
System.out.println("a age:" + a.getAge());
ShallowCopy b = new ShallowCopy();
b.setName(a.getName());
b.setAge(a.getAge());
System.out.println("b name:" + b.getName());
System.out.println("b age:" + b.getAge());
b.setAge(40);
System.out.println("b name:" + b.getName());
System.out.println("b age:" + b.getAge());
System.out.println("a name:" + a.getName());
System.out.println("a age:" + a.getAge());
}
}
B的改变,不会对A造成任何影响。
如果Java对象的属性都是只读类的话,如原始数据类型、数据封装类、String、BigDecimal、BigInteger等,那么浅层复制与深层复制达到的效果是一样的。如果属性有Date或其他自定的数据类,那么线层复制,B的修改可能导致A的改变。数据就串了。
package com.javaer.examples;
import java.util.Date;
/**
* java浅层复制的特点
*
* @author mc2
*
*/
public class ShallowCopy implements Cloneable {
private String name;
private int age;
private Date brith;
public ShallowCopy clone(){
try {
return (ShallowCopy)super.clone();
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public Date getBrith() {
return brith;
}
public void setBrith(Date brith) {
this.brith = brith;
}
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;
}
/**
* @param args
* @throws CloneNotSupportedException
*/
public static void main(String[] args) throws CloneNotSupportedException {
ShallowCopy a = new ShallowCopy();
a.setName("yuexiaosheng");
a.setAge(30);
a.setBrith(new Date());
System.out.println("a name:" + a.getName());
System.out.println("a age:" + a.getAge());
System.out.println("a brith:" + a.getBrith());
System.out.println("---------------------------------------" );
ShallowCopy b = new ShallowCopy();
b.setName(a.getName());
b.setAge(a.getAge());
b.setBrith(a.getBrith());
//ShallowCopy b = (ShallowCopy) a.clone();
System.out.println("b name:" + b.getName());
System.out.println("b age:" + b.getAge());
System.out.println("b brith:" + b.getBrith());
System.out.println("---------------------------------------" );
b.setAge(40);
//b.setBrith(new Date(100000L));
b.getBrith().setTime(100000L);
System.out.println("b name:" + b.getName());
System.out.println("b age:" + b.getAge());
System.out.println("b brith:" + b.getBrith());
System.out.println("---------------------------------------" );
System.out.println("a name:" + a.getName());
System.out.println("a age:" + a.getAge());
System.out.println("a brith:" + a.getBrith());
System.out.println("---------------------------------------" );
}
}
上面的例子,当使用b.setBrith(new Date(100000L)); 不会对A发生改变。但是如果b.getBrith().setTime(100000L);这样就会改变A
上面例子实现浅层复制办法有两个
1.ShallowCopy b = new ShallowCopy();
b.setName(a.getName());
b.setAge(a.getAge());
b.setBrith(a.getBrith());
2.ShallowCopy b = (ShallowCopy) a.clone();
那么如何实现深层复制?
将clone函数修改如下。
public ShallowCopy clone(){
ShallowCopy T = null;
try {
T = (ShallowCopy)super.clone();
T.brith = (Date)T.brith.clone();
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return T;
}
核心是增加了一个句话来clone属性
T.brith = (Date)T.brith.clone();
String[] ary1 = Arrays.copyOf(ary, ary.length);
This entry was posted in JAVA and tagged java, java教程. Bookmark the permalink.
月小升QQ 2651044202, 技术交流QQ群 178491360
首发地址:月小升博客 – https://java-er.com/blog/java-deep-copy/
无特殊说明,文章均为月小升原创,欢迎转载,转载请注明本文地址,谢谢