Java 浅拷贝、深拷贝

主要介绍Java中的浅拷贝和深拷贝概念以及实例演示

浅拷贝

    浅拷贝又叫浅克隆,克隆主要是针对对象来说的,即在原有的对象基础之上再克隆出一份同样的对象。

  • 1.实现Cloneable接口
  • 2.重写Object类的clone方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public class Student implements Cloneable{
private String name;
private int age;
public Student(){
}
public Student(String name, int age){
this.name = name;
this.age = age;
}

public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}

public int getAge(){
return this.age;
}
public void setAge(int age){
this.age = age;
}

//重写Object类的clone方法
protected Object clone(){
Object obj = null;
try{
//调用父类的clone方法
obj = super.clone();
}catch(CloneNotSupportedException e){
e.printStackTrace();
}
return obj;
}
}

public class ShallowCopy{
public static void main(String[] args){
Student student1 = new Strudent("zhangsan", 24);
Student student2 = (Student)student1.clone();

student2.setName("lisi");
System.out.println(student1.getName());
System.out.println(student2.getName());
}
}
最终测试结果输出如下:
zhangsan
lisi

深拷贝

    深拷贝又叫深克隆,深拷贝和浅拷贝一样都是在已生成的对象基础之上克隆(拷贝)一份新对象。拷贝的目的是使原对象
和拷贝之后的对象互补影响,即原对象发生变化时,不会影响到拷贝之后的对象。假如对象里面的属性都是基本类型,
浅拷贝即可完成此功能,但是如果对象属性是另外一个对象时,或者属性的类里面又包含其他对象时,浅拷贝不能完成
此功能,此时需要深拷贝。

  1. 基于序列化反序列化的方式实现深拷贝
  • 1.需要拷贝的对象要实现Serializable接口,不需要实现Cloneable接口,不需要重写Object类的clone方法.
  • 2.要拷贝的对象的属性是对象的时候,此属性对象也需要实现Serializable接口,以此类推.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//此类为要克隆对象的属性类,故要实现Serializable接口
public class Address implements Serializable{
private String name;

public void getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
}
public class Student implements Serializable{
private String name;
private int age;
private Address address;
public Student(){
}
public Student(String name, int age, Address address){
this.name = name;
this.age = age;
this.address = address;
}

public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}

public int getAge(){
return this.age;
}
public void setAge(int age){
this.age = age;
}

public Address getAddress(){
return this.address;
}
public void setAddress(Address address){
this.address = address;
}

//自己编写克隆方法
public Student deepClone(){
Student student = null;
ByteArrayOutputStream bos = null;
ObjectOutputStream oos = null;

ByteArrayInputStream bis = null;
ObjectInputStream ois = null;

try{
//将对象写入到字节数组输出流
bos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bos);
oos.writeObject(this);

//将字节数组对象转化为对象
bis = new ByteArrayInputStream(bos.toByteArray());
ois = new ObjectInputStream(bis);
student = (Student)ois.readObject();
}catch(Exception e){
e.printStackTrace();
}finally{
closeStream(bos,);
}
return Student;
}

private void closeStream(ByteArrayOutputStream bos, ObjectOutputStream oos,
ByteArrayInputStream bis, ObjectInputStream ois){
try{
if(null != bos){
bos.close();
}
}catch(Exception e){
e.printStackTrace();
}

try{
if(null != oos){
oos.close();
}
}catch(Exception e){
e.printStackTrace();
}

try{
if(null != bis){
bis.close();
}
}catch(Exception e){
e.printStackTrace();
}

try{
if(null != ois){
ois.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
}

public class DeepCopy{
public static void main(String[] args){
Address address = new Address();
address.setName("beijing");
Student student1 = new Strudent("zhangsan", 24, address);
Student student2 = (Student)student1.deepClone();

student2.setName("lisi");
student2.getAddress().setName("shanghai");
System.out.println(student1.getName() + ":" + student1.getAddress().getName());
System.out.println(student2.getName() + ":" + student2.getAddress().getName());
}
}
最终测试结果输出如下:
zhangsan:beijing
lisi:shanghai
坚持原创技术分享,您的支持将鼓励我的继续创作