隨著現代科技的發展,人類已經能夠複製(clone)牛,羊等動物。Java是物件導向,也能夠實現對象的複製(clone),在之前的學習中見到引用的賦值操作,如下例所示:
首先定義一個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;
}
protected 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();
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:xiaobai
s1.age:10
我們可以看出,student 和s1的內容完全一致,而且我們可以修改s1的值,而不會影響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,所以我們需要對該異常進行處理。以上的Student類中只有基本屬性,這種拷貝被稱為淺拷貝。
深拷貝
如果對象中有其他對象的引用,使用淺拷貝無法完成對象的整個複製,因為如果使用淺拷貝,只是對象的引用得到的拷貝,而兩個引用是指向了同一個對象,對其中一個修改還是會影響到另外一個對象。這時後我們需要引入深拷貝,深拷貝實現起來也比較簡單,只需要對對象中的對象再次進行clone操作。看以下執行個體
Glasses類代碼如下:
public class Glasses implements Cloneable {
private String color;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
在Student類中包含Glasses,代碼如下:
public class Student implements Cloneable{
private String studentName;
private Glasses glasses;
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 Glasses getGlasses() {
return glasses;
}
public void setGlasses(Glasses glasses) {
this.glasses = glasses;
}
protected Object clone() throws CloneNotSupportedException {
Student student = (Student) super.clone();
student.glasses= (Glasses) glasses.clone();
return student;
}
}
如果想對Glasses進行拷貝,而不是僅僅複製Glasses的引用,在clone方法中增加了student.glasses= (Glasses) glasses.clone();
入口函數:
public static void main(String args[]){
Student student = new Student();
student.setAge(10);
student.setStudentName("xiaobai");
Glasses glasses = new Glasses();
glasses.setColor("red");
student.setGlasses(glasses);
try {
Student s1 = (Student) student.clone();
s1.setStudentName("xiaohei");
System.out.println("s1.color:"+s1.getGlasses().getColor());
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
運行輸出:
s1.color:red
通過以上執行個體,可以看出student中Glasses的color值被完全複製給了s1,所以s1的Glasses的color也是red。