歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java中的克隆

Java中的克隆

日期:2017/3/1 9:13:16   编辑:Linux編程

1.為什麼要使用克隆,對象的引用賦值不是也可以嗎,下面會給出證明

首先定義一個Student類

public class Student implements Cloneable{

private String studentName;

private int age;

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public String getStudentName() {

return studentName;

}

public void setStudentName(String studentName) {

this.studentName = studentName;

}

}

入口函數:

public static void main(String args[]){

Student student0 = new Student();

student0.setStudentName("xiaoming");

Student student1 = student0;

System.out.println("student0.getStudentName:"+student0.getStudentName());

System.out.println("student1.getStudentName:"+student1.getStudentName());

}

運行結果:打印輸出:

student0.getStudentName:xiaoming

student1.getStudentName:xiaoming

在入口函數中student0 被付給了student1兩引用指向了內存中的同一塊空間,所以通過student0對對象的操作與通過student1對對象的操作完全一致。

有時候需要創造一個student0的副本,內容與student0完全一致,但是以後可以根據需要對student1內容修改,而不會影響到student0,這時候我們就需要用到clone.

雖然Clone方法在Object中存在的,但是如果想要調用clone必須實現Cloneable接口,否則會拋出java.lang.CloneNotSupportedException。

看一下實例

public class Student implements Cloneable{

private String studentName;

private int age;

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public String getStudentName() {

return studentName;

}

public void setStudentName(String studentName) {

this.studentName = studentName;

}

public Object clone() throws CloneNotSupportedException {

Student student = (Student) super.clone();

return student;

}

}

public static void main(String args[]){

Student student = new Student();

student.setAge(10);

student.setStudentName("xiaobai");

try {

Student s1 = (Student) student.clone();

s1.setStudentName("xiaohei");

System.out.println("student.studentName:"+student.getStudentName());

System.out.println("student.age:"+student.getAge());

System.out.println("s1.studentName:"+s1.getStudentName());

System.out.println("s1.age:"+s1.getAge());

} catch (CloneNotSupportedException e) {

e.printStackTrace();

}

}

運行結果:

student.studentName:xiaobai

student.age:10

s1.studentName:xiaohei

s1.age:10

我們發現s1的studentName值得到了修改,但是student的studentName值沒有受到影響,這就是clone的作用。使用起來很簡單,只需要在需要clone的對象上實現(implements)Cloneable接口,然後再在類中加上clone方法,在方法中只需要調用super.clone()即可,如上例中的部分代碼:Student student = (Student)super.clone(),需要注意clone方法在當前類沒有實現Cloneable的情況下可能拋出CloneNotSupportedException,所以我們需要對該異常進行處理。

2.總結Java的clone()方法
⑴clone方法將對象復制了一份並返回給調用者。一般而言,clone()方法滿足:
①對任何的對象x,都有x.clone() !=x//克隆對象與原對象不是同一個對象
②對任何的對象x,都有x.clone().getClass()= =x.getClass()//克隆對象與原對象的類型一樣
③如果對象x的equals()方法定義恰當,那麼x.clone().equals(x)應該成立。
⑵Java中對象的克隆
①為了獲取對象的一份拷貝,我們可以利用Object類的clone()方法。
②在派生類中覆蓋基類的clone()方法,並聲明為public。
③在派生類的clone()方法中,調用super.clone()。
④在派生類中實現Cloneable接口。

3.java中的深克隆與淺克隆

淺復制與深復制概念
⑴淺復制(淺克隆)
被復制對象的所有變量都含有與原來的對象相同的值,而所有的對其他對象的引用仍然指向原來的對象。換言之,淺復制僅僅復制所考慮的對象,而不復制它所引用的對象。

⑵深復制(深克隆)
被復制對象的所有變量都含有與原來的對象相同的值,除去那些引用其他對象的變量。那些引用其他對象的變量將指向被復制過的新對象,而不再是原

有的那些被引用的對象。換言之,深復制把要復制的對象所引用的對象都復制了一遍。

說明:
①為什麼我們在派生類中覆蓋Object的clone()方法時,一定要調用super.clone()呢?在運行時刻,Object中的clone()識別出你要復制的是哪一個對象,然後為此對象分配空間,並進行對象的復制,將原始對象的內容一一復制到新對象的存儲空間中。
②繼承自java.lang.Object類的clone()方法是淺復制。以下代碼可以證明之。

class Professor
{
String name;
int age;
Professor(String name,int age)
{
this.name=name;
this.age=age;
}
}
class Student implements Cloneable
{
String name;// 常量對象。
int age;
Professor p;// 學生1和學生2的引用值都是一樣的。
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;
System.out.println("name="+s1.p.name+","+"age="+s1.p.age);//學生1的教授成為lisi,age為30。
}

那應該如何實現深層次的克隆,即修改s2的教授不會影響s1的教授?代碼改進如下。
改進使學生1的Professor不改變(深層次的克隆)
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;
}
}
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;
System.out.println("name="+s1.p.name+","+"age="+s1.p.age);//學生1的教授不 改變。
}

4.利用串行化來做深復制

把對象寫到流裡的過程是串行化(Serilization)過程,但是在Java程序師圈子裡又非常形象地稱為“冷凍”或者“腌鹹菜(picking)”過程;而把對象從流中讀出來的並行化(Deserialization)過程則叫做 “解凍”或者“回鮮(depicking)”過程。

應當指出的是,寫在流裡的是對象的一個拷貝,而原對象仍然存在於JVM裡面,因此“腌成鹹菜”的只是對象的一個拷貝,Java鹹菜還可以回鮮。在Java語言裡深復制一個對象,常常可以先使對象實現Serializable接口,然後把對象(實際上只是對象的一個拷貝)寫到一個流裡(腌成鹹菜),再從流裡讀出來(把鹹菜回鮮),便可以重建對象。
如下為深復制源代碼。
public Object deepClone()
{
//將對象寫到流裡
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());
}

這樣做的前提是對象以及對象內部所有引用到的對象都是可串行化的,否則,就需要仔細考察那些不可串行化的對象可否設成transient,從而將之排除在復制過程之外。上例代碼改進如下。
class Teacher implements Serializable{
String name;
int age;
Teacher(String name,int age){
this.name=name;
this.age=age;
}
}
class Student implements Serializable{
String name;//常量對象
int age;
Teacher t;//學生1和學生2的引用值都是一樣的。
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;
System.out.println("name="+s1.t.name+","+"age="+s1.t.age);//學生1的老師不改變
}

Copyright © Linux教程網 All Rights Reserved