Java浅拷贝和深拷贝概念以及实现
在Java语言中,因为有引用类型的变量,所以也会有浅拷贝与深拷贝的区别
但是对于Java中的基础类型byte boolean char short int long float double以及它们的封装类型都实现了深拷贝
浅拷贝代码示例
packagepriv.jack.jdk.demo;/*** 浅拷贝
*@authorJack
**/
public classShallowCopy {public static void main(String[] args) throwsException {
Wallet wallet= new Wallet("LV", 10000);
Person p1= new Person("curry", 28, wallet);
Person p2=(Person) p1.clone();
p2.getWallet().setName("保罗");
p2.getWallet().setMoney(60);
p2.setName("stu");
p2.setAge(30);
System.out.println(p1.toString());
System.out.println(p2.toString());
}
}class Wallet implementsCloneable {privateString name;private longmoney;public Wallet(String name, intmoney) {this.name =name;this.money =money;
}
@OverridepublicString toString() {return "Wallet [name=" + name + ", money=" + money + "]";
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}public longgetMoney() {returnmoney;
}public void setMoney(longmoney) {this.money =money;
}
@Overrideprotected Object clone() throwsCloneNotSupportedException {return super.clone();
}
}class Person implementsCloneable {privateString name;private intage;privateWallet wallet;public Person(String name, intage, Wallet wallet) {this.name =name;this.age =age;this.wallet =wallet;
}
@OverridepublicString toString() {return "Person [name=" + name + ", age=" + age + ", p=" + wallet + "]";
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}public intgetAge() {returnage;
}public void setAge(intage) {this.age =age;
}publicWallet getWallet() {returnwallet;
}public voidsetWallet(Wallet wallet) {this.wallet =wallet;
}
@Overrideprotected Object clone() throwsCloneNotSupportedException {return super.clone();
}
}
运行结果如下
Person [name=curry, age=28, p=Wallet [name=保罗, money=60]]
Person [name=stu, age=30, p=Wallet [name=保罗, money=60]]
钱包的名称和金额都变了,说明两个人公用一个钱包,这样不好吧
深拷贝实例一
packagepriv.jack.jdk.demo;/*** 深拷贝
*@authorJack
**/
public classDeepCopy1 {public static void main(String[] args) throwsException {
Wallet1 Wallet1= new Wallet1("LV", 10000);
Person1 p1= new Person1("curry", 28, Wallet1);
Person1 p2=(Person1) p1.clone();
p2.getWallet1().setName("保罗");
p2.getWallet1().setMoney(60);
p2.setName("stu");
p2.setAge(30);
System.out.println(p1.toString());
System.out.println(p2.toString());
}
}class Wallet1 implementsCloneable {privateString name;private longmoney;public Wallet1(String name, intmoney) {this.name =name;this.money =money;
}
@OverridepublicString toString() {return "Wallet1 [name=" + name + ", money=" + money + "]";
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}public longgetMoney() {returnmoney;
}public void setMoney(longmoney) {this.money =money;
}
@Overrideprotected Object clone() throwsCloneNotSupportedException {return super.clone();
}
}class Person1 implementsCloneable {privateString name;private intage;privateWallet1 wallet;public Person1(String name, intage, Wallet1 Wallet1) {this.name =name;this.age =age;this.wallet =Wallet1;
}
@OverridepublicString toString() {return "Person1 [name=" + name + ", age=" + age + ", p=" + wallet + "]";
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}public intgetAge() {returnage;
}public void setAge(intage) {this.age =age;
}publicWallet1 getWallet1() {returnwallet;
}public voidsetWallet1(Wallet1 wallet) {this.wallet =wallet;
}
@Overrideprotected Object clone() throwsCloneNotSupportedException {
Person1 p=(Person1) super.clone() ;
p.wallet=(Wallet1) wallet.clone() ;returnp ;
}
}
运行结果如下:
Person1 [name=curry, age=28, p=Wallet1 [name=LV, money=10000]]
Person1 [name=stu, age=30, p=Wallet1 [name=保罗, money=60]]
深拷贝实例二
packagepriv.jack.jdk.demo;importjava.io.ByteArrayInputStream;importjava.io.ByteArrayOutputStream;importjava.io.ObjectInputStream;importjava.io.ObjectOutputStream;importjava.io.Serializable;/*** 深拷贝
*@authorJack
**/
public classDeepCopy2{public static void main(String[] args) throwsException {
Wallet2 wallet= new Wallet2("LV", 10000);
Person2 p1= new Person2("curry", 28, wallet);
Person2 p2=(Person2) p1.deepClone();
p2.getWallet2().setName("保罗");
p2.getWallet2().setMoney(60);
p2.setName("stu");
p2.setAge(30);
System.out.println(p1.toString());
System.out.println(p2.toString());
}
}class Wallet2 implementsSerializable {private static final long serialVersionUID = 5496170174896341934L;privateString name;private longmoney;public Wallet2(String name, intmoney) {this.name =name;this.money =money;
}
@OverridepublicString toString() {return "Wallet2 [name=" + name + ", money=" + money + "]";
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}public longgetMoney() {returnmoney;
}public void setMoney(longmoney) {this.money =money;
}
}class Person2 implementsSerializable {private static final long serialVersionUID = -7860166025209047988L;privateString name;private intage;privateWallet2 wallet;public Person2(String name, intage, Wallet2 Wallet2) {this.name =name;this.age =age;this.wallet =Wallet2;
}
@OverridepublicString toString() {return "Person2 [name=" + name + ", age=" + age + ", p=" + wallet + "]";
}public Object deepClone() throwsException {
ByteArrayOutputStream bao= newByteArrayOutputStream() ;
ObjectOutputStream oo= newObjectOutputStream(bao) ;
oo.writeObject(this);
ByteArrayInputStream bai= newByteArrayInputStream(bao.toByteArray()) ;
ObjectInputStream oi= newObjectInputStream(bai) ;
Object obj=oi.readObject() ;returnobj ;
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}public intgetAge() {returnage;
}public void setAge(intage) {this.age =age;
}publicWallet2 getWallet2() {returnwallet;
}public voidsetWallet2(Wallet2 wallet) {this.wallet =wallet;
}
}
运行结果如下:
Person2 [name=curry, age=28, p=Wallet2 [name=LV, money=10000]]
Person2 [name=stu, age=30, p=Wallet2 [name=保罗, money=60]]
OK,done。