加入收藏 | 设为首页 | 会员中心 | 我要投稿 鄂州站长网 (https://www.0711zz.com/)- 数据分析、网络、云渲染、应用安全、大数据!
当前位置: 首页 > 编程开发 > asp.Net > 正文

.net中的深拷贝与浅拷贝

发布时间:2020-07-16 08:45:54 所属栏目:asp.Net 来源:互联网
导读:publicoverrideEnemy Clone(bool isDeepCopy) { FootMan footman; if (isDeepCopy) { MemoryStream memoryStream = newMemoryStream(); BinaryFormatter formatter = newBinaryFormatter(); formatter.Serialize(memoryStream, this); memoryStream.Position

<p style="text-align: left" align="left"><span style="font-size: 10pt; font-family: Consolas"> <span style="font-size: 10pt; color: blue; font-family: Consolas">public<span style="font-size: 10pt; color: blue; font-family: Consolas">override<span style="font-size: 10pt; color: #2b91af; font-family: Consolas">Enemy<span style="font-size: 10pt; font-family: Consolas"> Clone(<span style="font-size: 10pt; color: blue; font-family: Consolas">bool<span style="font-size: 10pt; font-family: Consolas"> isDeepCopy)


<p style="text-align: left" align="left"><span style="font-size: 10pt; font-family: Consolas"> {


<p style="text-align: left" align="left"><span style="font-size: 10pt; font-family: Consolas"> <span style="font-size: 10pt; color: #2b91af; font-family: Consolas">FootMan<span style="font-size: 10pt; font-family: Consolas"> footman;


<p style="text-align: left" align="left"><span style="font-size: 10pt; font-family: Consolas"> <span style="font-size: 10pt; color: blue; font-family: Consolas">if<span style="font-size: 10pt; font-family: Consolas"> (isDeepCopy)


<p style="text-align: left" align="left"><span style="font-size: 10pt; font-family: Consolas"> {


<p style="text-align: left" align="left"><span style="font-size: 10pt; font-family: Consolas"> <span style="font-size: 10pt; color: #2b91af; font-family: Consolas">MemoryStream<span style="font-size: 10pt; font-family: Consolas"> memoryStream = <span style="font-size: 10pt; color: blue; font-family: Consolas">new<span style="font-size: 10pt; color: #2b91af; font-family: Consolas">MemoryStream<span style="font-size: 10pt; font-family: Consolas">();


<p style="text-align: left" align="left"><span style="font-size: 10pt; font-family: Consolas"> <span style="font-size: 10pt; color: #2b91af; font-family: Consolas">BinaryFormatter<span style="font-size: 10pt; font-family: Consolas"> formatter = <span style="font-size: 10pt; color: blue; font-family: Consolas">new<span style="font-size: 10pt; color: #2b91af; font-family: Consolas">BinaryFormatter<span style="font-size: 10pt; font-family: Consolas">();


<p style="text-align: left" align="left"><span style="font-size: 10pt; font-family: Consolas"> formatter.Serialize(memoryStream,<span style="font-size: 10pt; color: blue; font-family: Consolas">this<span style="font-size: 10pt; font-family: Consolas">);


<p style="text-align: left" align="left"><span style="font-size: 10pt; font-family: Consolas"> memoryStream.Position = 0;


<p style="text-align: left" align="left"><span style="font-size: 10pt; font-family: Consolas"> footman = (<span style="font-size: 10pt; color: #2b91af; font-family: Consolas">FootMan<span style="font-size: 10pt; font-family: Consolas">)formatter.Deserialize(memoryStream);


<p style="text-align: left" align="left"><span style="font-size: 10pt; font-family: Consolas"> }


<p style="text-align: left" align="left"><span style="font-size: 10pt; font-family: Consolas"> <span style="font-size: 10pt; color: blue; font-family: Consolas">else


<p style="text-align: left" align="left"><span style="font-size: 10pt; font-family: Consolas"> footman = (<span style="font-size: 10pt; color: #2b91af; font-family: Consolas">FootMan<span style="font-size: 10pt; font-family: Consolas">)<span style="font-size: 10pt; color: blue; font-family: Consolas">this<span style="font-size: 10pt; font-family: Consolas">.MemberwiseClone();


<p style="text-align: left" align="left"><span style="font-size: 10pt; font-family: Consolas"> <span style="font-size: 10pt; color: blue; font-family: Consolas">return<span style="font-size: 10pt; font-family: Consolas"> footman;


<p style="text-align: left" align="left"><span style="font-size: 10pt; font-family: Consolas"> }

说明:<span lang="EN-US" style="font-family: 微软雅黑; mso-bidi-font-size: 10.5pt">Clone()<span style="font-family: 微软雅黑; mso-bidi-font-size: 10.5pt">方法接受一个参数,表示是否是深拷贝。在这里,我们通过序列化反序列化实现深拷贝,深拷贝实现对象的完整复制,包括对象内部的引用类型都会复制一份全新的。在这里,如果<span lang="EN-US">3个敌人对象的<span lang="EN-US">Location都指向内存同一个地址的话,那么它们就分不开了,因此,在复制的时候需要进行深拷贝,使得它们的<span lang="EN-US">Location是独立的。由于进行了深拷贝,修改了一个敌人的位置并不会影响其它敌人。
<span lang="EN-US" style="font-family: 微软雅黑; mso-bidi-font-size: 10.5pt">.NET<span style="font-family: 微软雅黑; mso-bidi-font-size: 10.5pt">中使用<span lang="EN-US">Object的<span lang="EN-US">MemberwiseClone()方法来实现浅拷贝,通过序列化和反序列化实现深拷贝,后者代价比较大,选择何时的拷贝方式。<span lang="EN-US">

(编辑:鄂州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读