1.浅复制与深复制概念
浅复制(浅克隆)
被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用仍然指向原来的对象。换言之,浅复制仅仅复制所考虑的对象,而不复制它所引用的对象。
深复制(深克隆)
被复制对象的所有变量都含有与原来的对象相同的值,除去那些引用其他对象的变量。那些引用其他对象的变量将指向被复制过的新对象,而不再是原有的那些被引用的对象。换言之,深复制把要复制的对象所引用的对象都复制了一遍。
浅层克隆示例(实现Cloneable接口 并复写clone()方法即可)
public
class
Student
implements
Cloneable
{
String name;
int
age;
Student(String name,
int
age)
{
this
.name=name;
this
.age=age;
}
public
Object clone()
{
Object o=
null
;
try
{
o=(Student)
super
.clone();
//Object 中的clone()识别出你要复制的是哪一个对象。
}
catch
(CloneNotSupportedException e)
{
System.out.println(e.toString());
}
return
o;
}
public
static
void
main(String[] args)
{
Student s1=
new
Student(
"zhangsan"
,
18
);
Student s2=(Student)s1.clone();
s2.name=
"lisi"
;
s2.age=
20
;
//修改学生2后,不影响学生1的值。
System.out.println(
"name="
+s1.name+
","
+
"age="
+s1.age);
System.out.println(
"name="
+s2.name+
","
+
"age="
+s2.age);
}
}
实现cloneable接口深层复制的实现示例
class
Professor
implements
Cloneable
{
String name;
int
age;
Professor(String name,
int
age)
{
this
.name=name;
this
.age=age;
}
public
Object clone()
{
Object o=
null
;
try
{
o=
super
.clone();
}
catch
(CloneNotSupportedException e)
{
System.out.println(e.toString());
}
return
o;
}
}
public
class
Student
implements
Cloneable
{
String name;
int
age;
Professor p;
Student(String name,
int
age,Professor p)
{
this
.name=name;
this
.age=age;
this
.p=p;
}
public
Object clone()
{
Student o=
null
;
try
{
o=(Student)
super
.clone();
}
catch
(CloneNotSupportedException e)
{
System.out.println(e.toString());
}
//对引用的对象也进行复制
o.p=(Professor)p.clone();
return
o;
}
public
static
void
main(String[] args)
{
Professor p=
new
Professor(
"wangwu"
,
50
);
Student s1=
new
Student(
"zhangsan"
,
18
,p);
Student s2=(Student)s1.clone();
s2.p.name=
"lisi"
;
s2.p.age=
30
;
//学生1的教授不 改变。
System.out.println(
"name="
+s1.p.name+
","
+
"age="
+s1.p.age);
System.out.println(
"name="
+s2.p.name+
","
+
"age="
+s2.p.age);
}
}
利用串行化来做深复制(复制的对象实现Serializable接口)
class
Teacher
implements
Serializable{
String name;
int
age;
public
void
Teacher(String name,
int
age){
this
.name=name;
this
.age=age;
}
}
public
class
Student
implements
Serializable{
String name;
//常量对象
int
age;
Teacher t;
//学生1和学生2的引用值都是一样的。
public
void
Student(String name,
int
age,Teacher t){
this
.name=name;
this
.age=age;
this
.p=p;
}
public
Object deepClone()
throws
IOException,
OptionalDataException,ClassNotFoundException{
//将对象写到流里
ByteArrayOutoutStream bo=
new
ByteArrayOutputStream();
ObjectOutputStream oo=
new
ObjectOutputStream(bo);
oo.writeObject(
this
);
//从流里读出来
ByteArrayInputStream bi=
new
ByteArrayInputStream(bo.toByteArray());
ObjectInputStream oi=
new
ObjectInputStream(bi);
return
(oi.readObject());
}
public
static
void
main(String[] args){
Teacher t=
new
Teacher(
"tangliang"
,
30
);
Student s1=
new
Student(
"zhangsan"
,
18
,t);
Student s2=(Student)s1.deepClone();
s2.t.name=
"tony"
;
s2.t.age=
40
;
//学生1的老师不改变
System.out.println(
"name="
+s1.t.name+
","
+
"age="
+s1.t.age);
}
}