浅拷贝-深拷贝总结
淺拷貝-深拷貝總結(jié)
淺拷貝:
| 值傳遞 | 成員變量的引用值(內(nèi)存地址)復(fù)制(實際指向同一個) |
深拷貝:
| 值傳遞 | new一個新對象,進(jìn)行屬性賦值 |
淺拷貝兩種實現(xiàn)方法
1、拷貝構(gòu)造函數(shù)
一個學(xué)生類,包含兩個屬性,一個是基本數(shù)據(jù)類型name,一個是引用數(shù)據(jù)類型Age
結(jié)果
Boy{name='zyf', age=Age{age=99}} Boy{name='pmh', age=Age{age=99}}2、重寫clone()方法進(jìn)行淺拷貝
注意點:
1、protected native Object clone() throws CloneNotSupportedException;權(quán)限是受保護(hù)的,我們無法直接使用
2、使用clone方法的類必須實現(xiàn)Cloneable接口,否則會拋出異常CloneNotSupportedException
步驟如下
1、對象如果想通過clone方法實現(xiàn)淺拷貝,首先需要實現(xiàn)cloneable接口,重寫clone()方法
public class Student implements Cloneable {String name;Score score;public Student(String name, Score score) {this.name = name;this.score = score;}public Student() {}public String getName() {return name;}public void setName(String name) {this.name = name;}public Score getScore() {return score;}public void setScore(Score score) {this.score = score;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", score=" + score +'}';}@Overrideprotected Object clone() throws CloneNotSupportedException {return super.clone();} }class Score{int score;public Score(int score) {this.score = score;}public int getScore() {return score;}public void setScore(int score) {this.score = score;}@Overridepublic String toString() {return "Score{" +"score=" + score +'}';} } public class CopyCloneable {public static void main(String[] args) {Score score = new Score(100);Student student = new Student("pmh",score);try {//由此我們通過實現(xiàn)了Cloneabe的實現(xiàn)類Student重寫的clone方法實現(xiàn)了淺拷貝Student student1 = (Student)student.clone();System.out.println(student);System.out.println(student1);student.name = "ZYhf";score.score = 99;System.out.println(student);System.out.println(student1);} catch (CloneNotSupportedException e) {e.printStackTrace();}} }結(jié)果
Student{name='pmh', score=Score{score=100}} Student{name='pmh', score=Score{score=100}} Student{name='ZYhf', score=Score{score=99}} Student{name='pmh', score=Score{score=99}}深拷貝兩種實現(xiàn)方法
1、重寫clone方法來實現(xiàn)深拷貝
1、假設(shè)Student有兩個屬性,一個基本數(shù)據(jù)類型name,一個引用數(shù)據(jù)類型Socre,我們首先需要讓Score實現(xiàn)cloneable接口,重寫方法clone()
2、然后讓Student實現(xiàn)cloneable接口,實現(xiàn)clone方法,通過獲取拷貝的Student實例的Score屬性重新進(jìn)行一次拷貝
public class Student implements Cloneable {String name;Score score;public Student(String name, Score score) {this.name = name;this.score = score;}public Student() {}public String getName() {return name;}public void setName(String name) {this.name = name;}public Score getScore() {return score;}public void setScore(Score score) {this.score = score;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", score=" + score +'}';}@Overrideprotected Object clone() {Object obj = null;try {obj = super.clone();} catch (CloneNotSupportedException e) {e.printStackTrace();}Student stu = (Student) obj;stu.score= (Score) stu.getScore().clone();return obj;} }結(jié)果
Student{name='pmh', score=Score{score=100}} Student{name='pmh', score=Score{score=100}} Student{name='ZYhf', score=Score{score=99}} Student{name='pmh', score=Score{score=100}}雖然層次調(diào)用clone方法可以實現(xiàn)深拷貝,但是顯然代碼量實在太大。特別對于屬性數(shù)量比較多、層次比較深的類而言,每個類都要重寫clone方法太過繁瑣。
2、通過對象序列化實現(xiàn)深拷貝
結(jié)果
Girl{name='pmh', hobby=Hobby{hobby='乒乓球'}} Girl{name='pmh', hobby=Hobby{hobby='乒乓球'}} Girl{name='216', hobby=Hobby{hobby='羽毛器'}} Girl{name='pmh', hobby=Hobby{hobby='乒乓球'}}總結(jié)
- 上一篇: Xcode8 支持iOS7等设备调试的方
- 下一篇: dw01均衡电路_一种基于dw01的电池