C#中Clone一個(gè)對(duì)象的值到另一個(gè)對(duì)象案例
我也只是略懂皮毛,自己記錄下方便以后看的,各位有任何高見煩請(qǐng)留言,謝謝,抱拳!
想只復(fù)制值需要你的類實(shí)現(xiàn)ICloneable接口,并實(shí)現(xiàn)public object Clone()方法,其中
{ return this as object; //引用同一個(gè)對(duì)象 return this.MemberwiseClone(); //淺復(fù)制 return new DrawBase() as object;//深復(fù)制 }
引用
即相當(dāng)于用等號(hào)(=)賦值,相當(dāng)于 this,引用,兩者共用一套數(shù)據(jù)
深復(fù)制
新建對(duì)象,需手動(dòng)賦值,所有數(shù)據(jù)新建,兩個(gè)對(duì)象完全無(wú)關(guān)
淺復(fù)制
只復(fù)制值而不要引用,但只作用于基礎(chǔ)類型(int,float,string也好使),對(duì)于引用類型(如自建類)不生效。如果只想要值,可在自建類里也是用這種辦法,如a.t = T.Clone() as T; 最后將a返回,即可解決這個(gè)問(wèn)題。
本次主談淺復(fù)制,如有形如:
class a : ICloneable { public int i; public string str; public b ins; public object Clone() { return this.MemberwiseClone(); //淺復(fù)制 } } class b { public int bi; } --------------Program-------------- Main : { a t1 = new a(); t1.i = 1; t1.str = "str1"; t1.ins = new t1(); t1.ins.bi = 111; a t2 = t1.Clone() as a; print(t1.i + " " + t2.i); print(t1.str + " " + t2.str ); print(t1.ins.bi + " " + t2.ins.bi); t2.i = 2; t2.str = "str2"; t2.ins.bi = 222; print(t1.i + " " + t2.i); print(t1.str + " " + t2.str ); print(t1.ins.bi + " " + t2.ins.bi); }
首先這段代碼實(shí)際上是不會(huì)運(yùn)行的,因?yàn)闇\復(fù)制不會(huì)復(fù)制引用,也就是在print(t2.ins.bi)時(shí),實(shí)際上t2.ins是空,但t2.i和t2.str是有值的,而且在后續(xù)對(duì)t2.i和t2.str進(jìn)行改動(dòng)時(shí)并不會(huì)影響到t1的值,這一部分的結(jié)果使我們想要的。
但實(shí)際上類里有屬性是引用是難以避免甚至是很常見的,這種情況我們只需要在類b中也實(shí)現(xiàn)ICloneable接口并實(shí)現(xiàn)
public object Clone() { return this.MemberwiseClone(); //淺復(fù)制 }
同時(shí)修改類a中的Clone方法為:
public object Clone() { a T = this.MemberwiseClone() as a; //淺復(fù)制 T.ins = ins.Clone() as b; return T; }
由此,t2將獲取到t1的所有值,且對(duì)t2修改時(shí),并不會(huì)影響到t1。
補(bǔ)充知識(shí):C++中實(shí)現(xiàn)對(duì)象的clone()
在C#中,許多對(duì)象自動(dòng)實(shí)現(xiàn)了clone函數(shù),在C++中,要拷貝一個(gè)對(duì)象,除了自定義一個(gè)拷貝構(gòu)造函數(shù)來(lái)實(shí)現(xiàn)對(duì)象復(fù)制外,還可以像C#中那樣實(shí)現(xiàn)一個(gè)clone函數(shù),這需要借助編譯器實(shí)現(xiàn)的一個(gè)隱藏拷貝構(gòu)造函數(shù),這樣的做法,更省心。
#include "stdafx.h" #include <iostream> class CA { public: int value; CA* clone() const { return new CA( *this );} //僅一個(gè)構(gòu)造函數(shù) CA(int a ){value=a;} }; int _tmain(int argc, _TCHAR* argv[]) { CA* objA=new CA(10); CA* objtemp=objA->clone(); delete objA; std::cout<<objtemp->value; delete objtemp; return 0; }
以上這篇C#中Clone一個(gè)對(duì)象的值到另一個(gè)對(duì)象案例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
C# Distinct和重寫IEqualityComparer時(shí)要知道的二三事
這篇文章主要給大家介紹了關(guān)于C# Distinct和重寫IEqualityComparer時(shí)要知道的二三事,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06C#實(shí)現(xiàn)軟件開機(jī)自動(dòng)啟動(dòng)的兩種常用方法總結(jié)
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)軟件開機(jī)自動(dòng)啟動(dòng)的兩種常用方法,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2023-07-07C#隱藏手機(jī)號(hào)、郵箱等敏感信息的實(shí)現(xiàn)方法
這篇文章主要介紹了C#隱藏手機(jī)號(hào)、郵箱等敏感信息的實(shí)現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下2016-09-09Unity編輯器資源導(dǎo)入處理函數(shù)OnPostprocessAudio使用案例
這篇文章主要為大家介紹了Unity編輯器資源導(dǎo)入處理函數(shù)OnPostprocessAudio使用案例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08