java浅拷贝_Java浅拷贝和深拷贝概念以及实现

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。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值