Java中的拷貝方式分爲深拷貝和淺拷貝。簡單來講,深拷貝就是把一個對象中的全部值,若是被拷貝對象中有對其餘對象的引用,那麼這個引用指向的對象自己會被從新建立。淺拷貝和深拷貝相似,可是若是被拷貝對象中有對其餘對象的引用,只是這個引用會被拷貝,而不是這個被引用的對象。 html
http://bbs.itheima.com/thread-23776-1-1.html?fstgj java
之前的學習網站,-全套java視頻教程,須要的本身看下,能夠去這個網站下載,下載視頻免費,不須要註冊和作什麼任務 學習
提及來有點繞口,那麼咱們就看看下面的圖解吧: 網站
深拷貝: this
淺拷貝: spa
來看下面這段代碼: code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class Car { private String brand; private int price; public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class Person { private String name; private Car car; public String getName() { return name; } public void setName(String name) { this.name = name; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public class test { public static void main(String[] args) { Car car1 = new Car(); car1.setBrand("BMW"); car1.setPrice(10000); Person person1 = new Person(); person1.setCar(car1); person1.setName("person1"); Person person2 = person1; person2.setName("person2"); System.out.println(person1.getName()); // person2 System.out.println(person2.getName()); // person2 Car car2 = new Car(); car2.setBrand("Benz"); car2.setPrice(20000); person1.setCar(car2); System.out.println(person2.getCar().getBrand()); // Benz System.out.println(person2.getCar().getPrice()); // 20000 } } |
重點在Person person2 = person1;這一句上,person1裏面包括了一個對Car對象的引用,那麼這句話是深拷貝仍是淺拷貝呢?答案是什麼都不是。它只是一個簡單的引用傳遞,執行完這句話之後,person1和person2都指向了同一個person對象,因此不管誰去改變對象,另外一個引用再去調用該對象的值都會發生改變。 好吧,言歸正傳,下面來實現一個淺拷貝。 視頻
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 |
public class Person implements Cloneable{ private String name; private Car car; public String getName() { return name; } public void setName(String name) { this.name = name; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } public Object clone() { Person person = null; try { person = (Person) super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return person; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public class CloneTest { public static void main(String[] args) { Car car1 = new Car(); car1.setBrand("BMW"); car1.setPrice(10000); Person originalPerson = new Person(); originalPerson.setCar(car1); originalPerson.setName("originalPerson"); Person clonePerson = (Person) originalPerson.clone(); originalPerson.setName("originalPerson_1"); originalPerson.getCar().setBrand("Benz"); System.out.println(originalPerson.getName()); // originalPerson_1 System.out.println(originalPerson.getCar().getBrand()); // Benz System.out.println(clonePerson.getName()); // originalPerson System.out.println(clonePerson.getCar().getBrand()); // Benz } } |
Car類不變,Person實現了Cloneable接口,而後重載了父類的clone方法,而且直接調用super.clone()方法來拷貝。可是值得注意的是,父類的clone只是淺拷貝,因此纔會有上述的輸出結果。那麼,要想達到深拷貝,須要作些什麼呢? 其實答案已經很明顯了,由於clone是淺拷貝,而Car中都是原始類型的變量,因此咱們只須要讓Car類也實現Cloneable接口,而後重載clone方法,而後回到Person類中,在clone的時候,加上car = car.clone()就好了。 htm
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 |
package problems; public class Car implements Cloneable { private String brand; private int price; public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public Object clone() { Car car = null; try { car = (Car) super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return car; } } |
Person類中的clone: 對象
1 2 3 4 5 6 7 8 9 10 |
public Object clone() { Person person = null; try { person = (Person) super.clone(); car = (Car) car.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return person; } |