久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

ASP.NET堆和棧三之引用類型對(duì)象拷貝和內(nèi)存分配

瀏覽:115日期:2022-06-08 14:52:39

".NET的堆和棧"系列:

在" ASP.NET堆和棧一之基本概念和值類型內(nèi)存分配"中,了解了"堆"和"棧"的基本概念,以及值類型的內(nèi)存分配。我們知道:當(dāng)執(zhí)行一個(gè)方法的時(shí)候,值類型實(shí)例會(huì)在"棧"上分配內(nèi)存,而引用類型實(shí)例會(huì)在"堆"上分配內(nèi)存,當(dāng)方法執(zhí)行完畢,"棧"上的實(shí)例由操作系統(tǒng)自動(dòng)釋放,"堆"上的實(shí)例由.NET Framework的GC進(jìn)行回收。

在" ASP.NET堆和棧二之值類型和引用類型參數(shù)傳遞和內(nèi)存分配"中,我們了解了值類型參數(shù)和引用類型參數(shù)在傳遞時(shí)的內(nèi)存分配情況。

而本篇的重點(diǎn)要放在:引用類型對(duì)象拷貝以及內(nèi)存分配。

引用類型對(duì)象拷貝 成員都是值類型

public struct Shoe{    public string Color;} public class Dude{    public string Name;    public Shoe RightShoe;    public Shoe LeftShoe;        public Dude CopyDude()    {Dude newPerson = new Dude();newPerson.Name = Name;newPerson.LeftShoe = LeftShoe;newPerson.RightShoe = RightShoe; return newPerson;    }         public override string ToString()    {return (Name + " : Dude!, I have a " + RightShoe.Color  +" shoe on my right foot, and a " +LeftShoe.Color + " on my left foot.");    } }public static void Main(){    Dude Bill = new Dude();    Bill.Name = "Bill";    Bill.LeftShoe = new Shoe();    Bill.RightShoe = new Shoe();    Bill.LeftShoe.Color = Bill.RightShoe.Color = "Blue";         Dude Ted =  Bill.CopyDude();    Ted.Name = "Ted";    Ted.LeftShoe.Color = Ted.RightShoe.Color = "Red";         Console.WriteLine(Bill.ToString());    Console.WriteLine(Ted.ToString());    }

輸出結(jié)果:
Bill : Dude!, I have a Red shoe on my right foot, and a Red on my left foot
Ted : Dude!, I have a Red shoe on my right foot, and a Red on my left foot

以上,當(dāng)引用類型的屬性、成員都是值類型的時(shí)候,拷貝是完全拷貝。

引用類型對(duì)象拷貝 包含引用類型成員

把Shoe由struct值類型改成引用類型class。

public class Shoe{    public string Color;}

再次運(yùn)行,輸出結(jié)果:
Bill : Dude!, I have a Red shoe on my right foot, and a Red on my left foot
Ted : Dude!, I have a Red shoe on my right foot, and a Red on my left foot

當(dāng)Dude類包含引用類型屬性Shoe的時(shí)候,在托管堆上的情況是這樣的:

拷貝后,2個(gè)Dude的Shoe類型的屬性指向了同一個(gè)托管堆內(nèi)的Shoe實(shí)例,改變Shoe的值會(huì)同時(shí)影響到2個(gè)Dude。

很顯然,這不是我們期望的完全拷貝,如何做到完全拷貝呢?
--實(shí)現(xiàn)ICloneable接口

ICloneable接口的Clone()方法,允許我們?cè)诳截惖臅r(shí)候,進(jìn)行一些自定義設(shè)置。

讓引用類Shoe實(shí)現(xiàn)ICloneable接口。

public class Shoe : ICloneable{    public string Color;         public object Clone()    {Shoe newShoe = new Shoe();newShoe.Color = Color.Clone() as string;return newShoe;    }}

以上,Shoe的string類型屬性Color之所以可以使用Color.Clone()方法,是因?yàn)閟tring也實(shí)現(xiàn)了ICloneable接口;又由于Clone()返回類型是object,所以,在使用Color.Clone()方法之后,需要把object轉(zhuǎn)換成string類型。

現(xiàn)在,在Dude類的CopyDude()方法中,當(dāng)拷貝Shoe類型屬性的時(shí)候,就可以使用Shoe獨(dú)有的拷貝方法Clone()。

public Dude CopyDude(){    Dude newPerson = new Dude();    newPerson.Name = Name;    newPerson.LeftShoe = LeftShoe.Clone() as Shoe;    newPerson.RightShoe = RightShoe.Clone() as Shoe;        return newPerson;}

客戶端程序:

public static void Main(){    Dude Bill = new Dude();    Bill.Name = "Bill";    Bill.LeftShoe = new Shoe();    Bill.RightShoe = new Shoe();    Bill.LeftShoe.Color = Bill.RightShoe.Color = "Blue";         Dude Ted =  Bill.CopyDude();    Ted.Name = "Ted";    Ted.LeftShoe.Color = Ted.RightShoe.Color = "Red";         Console.WriteLine(Bill.ToString());    Console.WriteLine(Ted.ToString());    }

輸出結(jié)果:
Bill : Dude!, I have a Blue shoe on my right foot, and a Blue on my left foot
Ted : Dude!, I have a Red shoe on my right foot, and a Red on my left foot

這正是我們期望的完全拷貝!

完全拷貝,托管堆上的情況是這樣的:

當(dāng)然也可以讓同時(shí)包含值類型和引用類型成員,同時(shí)需要拷貝的類實(shí)現(xiàn)ICloneable接口。

public class Dude: ICloneable{    public string Name;    public Shoe RightShoe;    public Shoe LeftShoe;         public override string ToString()    {return (Name + " : Dude!, I have a " + RightShoe.Color  +    " shoe on my right foot, and a " +    LeftShoe.Color + " on my left foot.");    }    #region ICloneable Members         public object Clone()    {Dude newPerson = new Dude();newPerson.Name = Name.Clone() as string;newPerson.LeftShoe = LeftShoe.Clone() as Shoe;newPerson.RightShoe = RightShoe.Clone() as Shoe; return newPerson;    }         #endregion}

客戶端調(diào)用:

public static void Main(){    Class1 pgm = new Class1();         Dude Bill = new Dude();    Bill.Name = "Bill";    Bill.LeftShoe = new Shoe();    Bill.RightShoe = new Shoe();    Bill.LeftShoe.Color = Bill.RightShoe.Color = "Blue";        Dude Ted =  Bill.Clone() as Dude;    Ted.Name = "Ted";    Ted.LeftShoe.Color = Ted.RightShoe.Color = "Red";         Console.WriteLine(Bill.ToString());    Console.WriteLine(Ted.ToString());     }

輸出結(jié)果:
Bill : Dude!, I have a Blue shoe on my right foot, and a Blue on my left foot.
Ted : Dude!, I have a Red shoe on my right foot, and a Red on my left foot.

也是我們期望的完全拷貝!

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接

標(biāo)簽: ASP.NET
相關(guān)文章:
主站蜘蛛池模板: 视频在线一区 | 不卡二区| 91精品国产一区二区三区蜜臀 | 在线观看免费国产 | 亚洲成人久久久 | 日韩精品1区2区 | 91免费看网站 | 成版人性视频 | 精品成人一区 | 国产一区二区黑人欧美xxxx | 国产精一区二区 | 亚洲骚片 | 色免费视频| 成人深夜小视频 | h在线观看 | 欧美精品一区在线发布 | baoyu133. con永久免费视频 | 亚洲777| 一级高清 | 9久久婷婷国产综合精品性色 | 国产精品久久久久国产a级 99精品欧美一区二区三区综合在线 | 国产精品12 | 17c一起操 | 精品亚洲一区二区三区四区五区 | 亚洲精品中文字幕中文字幕 | 亚洲成人二区 | 日本一二三视频 | 国产高潮呻吟久久渣男片 | 国产成人精品综合 | 色婷婷综合久久 | 色综合国产| 亚洲成人av | 久久久国产一区二区三区 | 97国产精品久久久 | 国内成人精品2018免费看 | 国产91久久精品 | 中文字幕av网 | 久久精品网 | 免费视频久久久久 | 特级理论片 | 天堂一区 |