java深拷贝和浅拷贝_Java深拷贝和浅拷贝的区别

浅拷贝

被复制的对象的所有的变量都与原对象有相同的值,而所有的引用对象仍然指向原来的对象。换言之,浅拷贝 不复制引用对象。

48304ba5e6f9fe08f3fa1abda7d326ab.png

1 class Experience {

2 private String skill;

3 public void setSkill(String skill){

4 this.skill = skill;

5 }

6 public void setExperience(String skill){

7 this.skill = skill;

8 }

9

10 @Override

11 public String toString() {

12 return skill;

13 }

14 }

15

16 public class CloneTest implements Cloneable{

17

18 private int age ;

19 private Experience experience;

20

21 public CloneTest(){

22 this.age = 10;

23 this.experience = new Experience();

24 }

25 public Experience getExperience() {

26 return experience;

27 }

28 public void setExperience(String skill){

29 experience.setExperience(skill);

30 }

31 public void show(){

32 System.out.println(experience.toString());

33 }

34 public int getAge() {

35 return age;

36 }

37 @Override

38 protected Object clone() throws CloneNotSupportedException {

39 return (CloneTest) super.clone();40 }

41 }

42 //测试类

43 class MianTest{

44

45 public static void main(String[] args) throws CloneNotSupportedException {

46 CloneTest test = new CloneTest();

47 test.setExperience("我是小明,我精通Java,C++的复制粘贴");

48 test.show();

49 CloneTest cloneTest = (CloneTest) test.clone();

50 cloneTest.show();

51 cloneTest.setExperience("我是小明的副本,我精通Java,C++");

52 cloneTest.show();

53 test.show();54 System.out.println(cloneTest.getAge());

55 }

56 }

48304ba5e6f9fe08f3fa1abda7d326ab.png

输出的结果:

我是小明,我精通Java,C++的复制粘贴

我是小明,我精通Java,C++的复制粘贴

我是小明的副本,我精通Java,C++

我是小明的副本,我精通Java,C++

10

从结果中不难看出,拷贝的副本改变了Experience的skill属性,原类中的skill属性打印出来也是修改后的结果,说明引用 类型的拷贝没有将对象拷贝,引用的指向还是原类中的指向

深拷贝

除了被复制的对象的所有变量都有原来对象的值之外,还把引用对象也指向了被复制的新对象

48304ba5e6f9fe08f3fa1abda7d326ab.png

1 class Experience implements Cloneable{

2 private String skill;

3 public void setSkill(String skill){

4 this.skill = skill;

5 }

6 public void setExperience(String skill){

7 this.skill = skill;

8 }

9

10 @Override

11 public String toString() {

12 return skill;

13 }

14

15 @Override

16 protected Object clone() throws CloneNotSupportedException {

17 return super.clone();

18 }

19 }

20

21 public class CloneTest implements Cloneable{

22

23 private int age ;

24 private Experience experience;

25

26 public CloneTest(){

27 this.age = 10;

28 this.experience = new Experience();

29 }

30 public Experience getExperience() {

31 return experience;

32 }

33 public void setExperience(String skill){

34 experience.setExperience(skill);

35 }

36 public void show(){

37 System.out.println(experience.toString());

38 }

39 public int getAge() {

40 return age;

41 }

42 @Override

43 protected Object clone() throws CloneNotSupportedException {

44 CloneTest o = (CloneTest) super.clone();

45 o.experience = (Experience) o.getExperience().clone();46 return o;

47 }

48 }

49 class MianTest{

50

51 public static void main(String[] args) throws CloneNotSupportedException {

52 CloneTest test = new CloneTest();

53 test.setExperience("我是小明,我精通Java,C++的复制粘贴");

54 test.show();

55 CloneTest cloneTest = (CloneTest) test.clone();

56 cloneTest.show();

57 cloneTest.setExperience("我是小明的副本,我精通Java,C++");

58 cloneTest.show();

59 test.show();60 System.out.println(cloneTest.getAge());

61 }

62 }

48304ba5e6f9fe08f3fa1abda7d326ab.png

输出的结果:

我是小明,我精通Java,C++的复制粘贴

我是小明,我精通Java,C++的复制粘贴

我是小明的副本,我精通Java,C++

我是小明,我精通Java,C++的复制粘贴

10

可以看出和第一次的结果不同了。

o.experience =(Experience) o.getExperience().clone();

加了这句之后效果不同的。说明实现了深拷贝,原引用对象也复制过来了

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值