请问如何详细解释C语言编程?
- 内容介绍
- 文章标签
- 相关推荐
本文共计582个文字,预计阅读时间需要3分钟。
前言:我们都知道memberwise clone会进行浅拷贝。
什么是浅拷贝?
浅拷贝是指拷贝对象时,只复制对象本身及其包含的基本数据类型的值,而不复制对象所引用的其他对象。
如何实现深拷贝?
深拷贝是指拷贝对象时,不仅复制对象本身及其包含的基本数据类型的值,还要复制对象所引用的其他对象。
正文:
javapublic class Good { private Good oneClass=new Good(); private static Good __good; private static Good _good;}
前言
我们都知道memberwiseclone 会将浅克隆。
什么是浅克隆?如何深克隆呢?
正文
public class good{ private good(){ oneclass=new class{ int id=8; string name='id'; } } private static good __good; private static good __good=new good(); public good createinstance() { return __good.memberwiseclone(); } public int a=0; public string b="ab"; pulic class oneclass; }
测试:
void main() { var student1=good.createinstance(); var student2=good.createinstance(); student1.oneclass.id=9; console.log('student2 oneclass.id{0}',student2.oneclass.id); }
这里我们得出了结果为9;
ok,那么这真的是个匪夷所思的问题,明明两个对象啊。
那么回归到浅克隆上。
当克隆good的时候是这样的。
让good的classone的引用给了新的克隆对象。
那么如何深克隆呢?
深克隆其实就是将对象序列化,也就是说要深克隆的话必须对象系列化;
public class SerializeHelper { public static string Serializable(object target) { using (MemoryStream steam=new MemoryStream()) { new BinaryFormatter().Serialize(steam,target); return Convert.ToBase64String(steam.ToArray()); } } public static T Derializable<T>(string target) { byte[] targetArray = Convert.FromBase64String(target); using (MemoryStream steam =new MemoryStream(targetArray)) { return (T)(new BinaryFormatter().Deserialize(steam)); } } public static T DeepClone<T>(T t) { return Derializable<T>(Serializable(t)); } }
改变一个good 类。
public class good{ private good(){ oneclass=new class{ int id=8; string name='id'; } } private static good __good; private static good __good=new good(); public good createinstance() { return SerializeHelper.DeepClone(__good.memberwiseclone()); } public int a=0; public string b="ab"; pulic class oneclass; }
测试一下:
void main() { var student1=good.createinstance(); var student2=good.createinstance(); student1.oneclass.id=9; console.log('student2 oneclass.id{0}',student2.oneclass.id); }
以上就是详解c# 深克隆与浅克隆的详细内容,更多关于c# 深克隆与浅克隆的资料请关注易盾网络其它相关文章!
本文共计582个文字,预计阅读时间需要3分钟。
前言:我们都知道memberwise clone会进行浅拷贝。
什么是浅拷贝?
浅拷贝是指拷贝对象时,只复制对象本身及其包含的基本数据类型的值,而不复制对象所引用的其他对象。
如何实现深拷贝?
深拷贝是指拷贝对象时,不仅复制对象本身及其包含的基本数据类型的值,还要复制对象所引用的其他对象。
正文:
javapublic class Good { private Good oneClass=new Good(); private static Good __good; private static Good _good;}
前言
我们都知道memberwiseclone 会将浅克隆。
什么是浅克隆?如何深克隆呢?
正文
public class good{ private good(){ oneclass=new class{ int id=8; string name='id'; } } private static good __good; private static good __good=new good(); public good createinstance() { return __good.memberwiseclone(); } public int a=0; public string b="ab"; pulic class oneclass; }
测试:
void main() { var student1=good.createinstance(); var student2=good.createinstance(); student1.oneclass.id=9; console.log('student2 oneclass.id{0}',student2.oneclass.id); }
这里我们得出了结果为9;
ok,那么这真的是个匪夷所思的问题,明明两个对象啊。
那么回归到浅克隆上。
当克隆good的时候是这样的。
让good的classone的引用给了新的克隆对象。
那么如何深克隆呢?
深克隆其实就是将对象序列化,也就是说要深克隆的话必须对象系列化;
public class SerializeHelper { public static string Serializable(object target) { using (MemoryStream steam=new MemoryStream()) { new BinaryFormatter().Serialize(steam,target); return Convert.ToBase64String(steam.ToArray()); } } public static T Derializable<T>(string target) { byte[] targetArray = Convert.FromBase64String(target); using (MemoryStream steam =new MemoryStream(targetArray)) { return (T)(new BinaryFormatter().Deserialize(steam)); } } public static T DeepClone<T>(T t) { return Derializable<T>(Serializable(t)); } }
改变一个good 类。
public class good{ private good(){ oneclass=new class{ int id=8; string name='id'; } } private static good __good; private static good __good=new good(); public good createinstance() { return SerializeHelper.DeepClone(__good.memberwiseclone()); } public int a=0; public string b="ab"; pulic class oneclass; }
测试一下:
void main() { var student1=good.createinstance(); var student2=good.createinstance(); student1.oneclass.id=9; console.log('student2 oneclass.id{0}',student2.oneclass.id); }
以上就是详解c# 深克隆与浅克隆的详细内容,更多关于c# 深克隆与浅克隆的资料请关注易盾网络其它相关文章!

