java clone和深度複製和淺複製的個人總結

來源:互聯網
上載者:User
1.clone() 和“=”的比較

在基本類型變數裡的賦值如: int  a = 1;int b = a;a與b有相同的值,改變a或b的值不影響彼此。

但對象間的:

java.util.Date date1 = new java.util.Date();java.util.Date date2 = date1;

date1 與date2指向相同的對象,指向相同的儲存空間。就像小明的外號叫明明,小明和明明都指向同一個人。可以通過改變date1 或date2 的值,都會使對象發生改變。

使用clone就是區別於上面的引用複製。

java.util.Date date1 = new java.util.Date();java.util.Date date2 = (java.util.Date)date1.clone;

date1 與date2指向不相同的對象,但資料是相同的,就像是兩個衣著相同的不同的人。date1 與date2間的操作互不影響,他們有各自的儲存空間。可見,通過date1.clone()為date3 new了一個對象。

public class Test {public static void main(String[] args) {// TODO Auto-generated method stubjava.util.Date date1 = new java.util.Date();java.util.Date date2 = date1;java.util.Date date3 =(java.util.Date) date1.clone();System.out.println("date1 == date2?"+(date1 == date2));System.out.println("date1 == date2?"+(date1 == date3));}}
輸出結果:date1 == date2    true(使用“=”)
                    date1 == date3    false(使用clone())

2.clone()和Cloneable介面。 所有類都預設繼承了object類,object 類中定義有clone()方法,傳回型別:object,修飾符為protect ,子類可用。所以無法對類的變數直接使用clone方法(如:定義了某個類Student,聲明變數aStudent並建立了對象,但無法使用aStudent.clone())。 實現clone()的方法:要通過類中重寫clone的方法,修飾符改為public。除此之外,還必須實現Cloneable介面,否則會拋出異常(詳細看別人的總結 http://onlylove.iteye.com/blog/265113,我水平還不行,借鑒學習  )。上面的Date類可以直接使用clone()方法,是因為預設實現了Cloneable,另外還有ArrayList,Calendar類可直接使用。

class Student implements Cloneable {int number;String name;@Overridepublic Object clone() throws CloneNotSupportedException{return super.clone();}}
Student 類實現了介面,有複製的方法。 3.clone()的深淺複製。 在一個類中,如果資料域全都是基本類型,無需考慮深淺複製。

如果資料域含有對象,則考慮深淺複製的問題了。

class Student implements Cloneable {
int number;
String name;
java.util.Date date;

    @Overridepublic Object clone() throws CloneNotSupportedException{return super.clone();}

} 該類中有Date類的對象,如果直接使用上面的方法實現clone(),基本類型的變數都還是實現clone,但類型為對象的date1複製的只是個引用。 Student student1 = new Student(); Student student2 = student1.clone(); 比較: student1.number == student2.number      false student1.name == student2.name             false
student1.date == student2.date                 true
  這就是 淺複製,date 並沒有實現複製,而是複製了引用。相當於“=”。此時與上面總結1一樣,student1 和 student2的date指向同一個對象。 深度複製 深度複製就是實現淺複製中date沒有複製的情況。 將上面的Student類的clone()方法重寫:

class Student implements Cloneable {int number;String name;java.util.Date date;@Overridepublic Object clone() throws CloneNotSupportedException {Student studentClone = (Student) super.clone();studentClone.date = (java.util.Date) this.date.clone();return studentClone;}}
此時
Student student1 = new Student(); Student student2 = student1.clone(); 比較: student1.number == student2.number      false student1.name == student2.name             false
student1.date == student2.date                 false 實現了該類的深度複製。student1 和 student2的date指向不同一個對象。 (如有錯誤,歡迎指正~)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.