浅层复制的时候,一个对象仅仅在引用另外一个对象。属性没被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 classShallowCopy {
private Stringname;
private intage;
public StringgetName() {
return name;
}
public voidsetName(Stringname) {
this.name = name;
}
public intgetAge() {
return age;
}
public voidsetAge(intage) {
this.age = age;
}
/**
* @param args
*/
public staticvoid main(String[] args) {
ShallowCopya = newShallowCopy();
a.setName("yuexiaosheng");
a.setAge(30);
System.out.println("a name:" +a.getName());
System.out.println("a age:" +a.getAge());
ShallowCopyb = newShallowCopy();
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 classShallowCopy implements Cloneable {
private Stringname;
private intage;
private Datebrith;
public ShallowCopy clone(){
try {
return (ShallowCopy)super.clone();
} catch (CloneNotSupportedExceptione) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public DategetBrith() {
return brith;
}
public voidsetBrith(Datebrith) {
this.brith = brith;
}
public StringgetName() {
return name;
}
public voidsetName(Stringname) {
this.name = name;
}
public intgetAge() {
return age;
}
public voidsetAge(intage) {
this.age = age;
}
/**
* @param args
* @throws CloneNotSupportedException
*/
public staticvoid main(String[] args) throws CloneNotSupportedException {
ShallowCopya = newShallowCopy();
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("---------------------------------------" );
ShallowCopyb = newShallowCopy();
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(newDate(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(){
ShallowCopyT = null;
try {
T = (ShallowCopy)super.clone();
T.brith = (Date)T.brith.clone();
} catch (CloneNotSupportedExceptione) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return T;
}
核心是增加了一个句话来clone属性
T.brith = (Date)T.brith.clone();
java数据中常见的浅层复制方式
String[] ary = {“Tom”,”Jerry”};
String[] ary1 = Arrays.copyOf(ary, ary.length);