深拷貝和淺拷貝 完整例項

2021-10-23 04:06:09 字數 4440 閱讀 2595

1、淺拷貝:對基本資料型別進行值傳遞,對引用資料型別進行引用傳遞般的拷貝,此為淺拷貝。

2、深拷貝:對基本資料型別進行值傳遞,對引用資料型別,建立乙個新的物件,並複製其內容,此為深拷貝。

3、淺拷貝(clone)示例**

public

class

student

implements

cloneable

public

void

setname

(string name)

@override

public string tostring()

@override

public object clone()

throws clonenotsupportedexception

}

public

class

teacher

implements

cloneable

public

void

setname

(string name)

public student getstudent()

public

void

setstudent

(student student)

@override

public string tostring()

@override

public object clone()

throws clonenotsupportedexception

}

public

class

test

catch

(clonenotsupportedexception e)

system.out.

println

(xlaoshi)

; system.out.

println

(wanglaoshi);/*

teacher [name=王, student=student [name=張三]]

teacher [name=王, student=student [name=張三]]

*/// 修改原老師的姓名和學生的姓名,複製的x老師的老師姓名沒有跟著變化,學生姓名跟著原老師的學生姓名變化,證明是淺拷貝。

wanglaoshi.

setname

("張");

wanglaoshi.

getstudent()

.setname

("李四");

system.out.

println

(xlaoshi)

; system.out.

println

(wanglaoshi);/*

teacher [name=王, student=student [name=李四]]

teacher [name=張, student=student [name=李四]]

*/}}

4、深拷貝(clone)示例**

public

class

student

implements

cloneable

public

void

setname

(string name)

@override

public string tostring()

@override

public student clone()

throws clonenotsupportedexception

}

public

class

teacher

implements

cloneable

public

void

setname

(string name)

public student getstudent()

public

void

setstudent

(student student)

@override

public string tostring()

@override

public teacher clone()

throws clonenotsupportedexception

}

public

class

test

catch

(clonenotsupportedexception e)

system.out.

println

(xlaoshi)

; system.out.

println

(wanglaoshi);/*

teacher [name=王, student=student [name=張三]]

teacher [name=王, student=student [name=張三]]

*/// 修改原老師的姓名和學生的姓名,複製的x老師的老師姓名,學生姓名沒有跟著變化,證明是深拷貝。

wanglaoshi.

setname

("張");

wanglaoshi.

getstudent()

.setname

("李四");

system.out.

println

(xlaoshi)

; system.out.

println

(wanglaoshi);/*

teacher [name=王, student=student [name=張三]]

teacher [name=張, student=student [name=李四]]

*/}}

5、深拷貝(序列化)示例**

public

class

student

implements

serializable

public

void

setname

(string name)

@override

public string tostring()

}

public

class

teacher

implements

serializable

public

void

setname

(string name)

public student getstudent()

public

void

setstudent

(student student)

@override

public string tostring()

}

public

class

test

catch

(exception e)

}}

6、深拷貝(自定義)示例**

public

class

student

public

void

setname

(string name)

@override

public string tostring()

}

public

class

teacher

public

void

setname

(string name)

public student getstudent()

public

void

setstudent

(student student)

public teacher copy

(teacher teacher)

@override

public string tostring()

}

public

class

test

}

iOS 深拷貝淺拷貝例項

nsstring string 漢斯哈哈哈 沒有產生新物件 nsstring copystring string copy 產生新物件 nsmutablestring mutablecopystring string mutablecopy nslog string p copystring p m...

深拷貝和淺拷貝

淺拷貝就是物件的資料成員之間的簡單賦值,如你設計了乙個沒有類而沒有提供它的複製建構函式,當用該類的乙個物件去給令乙個物件賦值時所執行的過程就是淺拷貝,如 class a a private int data int main 這一句b a 就是淺拷貝,執行完這句後b.data 5 如果物件中沒有其他...

淺拷貝和深拷貝

以下情況都會呼叫拷貝建構函式 乙個物件以值傳遞的方式傳入函式體 例如 已知class a,class b void func a a void func a a func b b 此時函式對b的操作是呼叫拷貝建構函式後的臨時拷貝物件。多數傳指標 乙個物件以值傳遞的方式從函式返回 如 return b...