一个C#中浅复制(shallow copy)/深复制(deep copy)的例子
来源:互联网 发布:淘宝p图软件手机软件 编辑:程序博客网 时间:2024/04/29 14:55
[Serializable] public class User : ICloneable { public string name; public string desc; public string remark; public User() { } protected User(User u) { name = u.name; desc = u.desc; remark = u.remark; } object ICloneable.Clone() { return base.MemberwiseClone(); } public User Clone() { return base.MemberwiseClone() as User; //return new User(this); } public User Clone(bool IsDeepCopy) { using (MemoryStream ms = new MemoryStream(1000)) { BinaryFormatter bf = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.Clone)); bf.Serialize(ms, this); ms.Seek(0, SeekOrigin.Begin); User u = bf.Deserialize(ms) as User; ms.Close(); return u; } } } public class Guest : User { public string IPAddress; public Guest() { } public Guest(User u) : base(u) {} public Guest(Guest g) : base(g) { IPAddress = g.IPAddress; } }
User admin = new User() { name = "admin", desc = "管理员", remark = "管理员" }; User admin1 = new User() { name = "admin1", desc = "管理员", remark = "管理员" }; /*问题1:如果大多数属性一样,只有一个属性不一样。我想直接复制对象里的属性,然后再修改不一样的属性, 我可以这样写吗?如下: User admin2 = admin; admin2.name = "admin2";*/ User admin2 = admin.Clone(); admin2.name = "admin2"; /*问题二:我现在想通过子类去复制父类的属性,然后再增加自己的属性,可以吗?比如: User guest = new User() { name = "guste", desc = "游客", remark = "游客" }; Guest guest1 = guest; guest1.IPAddress = "192.168.168.3"; Guest guest2 = guest; guest1.IPAddress = "192.168.168.5";*/ User guest = new User() { name = "guste", desc = "游客", remark = "游客" }; Guest guest1 = new Guest( guest); guest1.name = "guest1"; guest1.IPAddress = "192.168.168.3"; Guest guest2 = new Guest(guest); guest2.name = "guest2"; guest2.IPAddress = "192.168.168.5";
- 一个C#中浅复制(shallow copy)/深复制(deep copy)的例子
- 浅复制(Shallow Copy)与深复制(Deep Copy)
- Difference between shallow copy and deep copy? 浅复制和深复制的区别
- Deep copy & Shallow copy
- Shallow copy & Deep copy
- java 中 的 deep copy 与 shallow copy
- shallow copy 和 deep copy 的示例
- 浅拷贝和深拷贝(shallow copy VS deep copy )
- 深拷贝与浅拷贝(Deep Copy and Shallow Copy)
- 深拷贝与浅拷贝(Deep Copy and Shallow Copy)
- iOS 浅拷贝(Shallow Copy)与深拷贝(Deep Copy)
- Deep copy(深拷贝)和shallow copy(浅拷贝)的区别
- Deep Copy and Shallow Copy
- deep copy vs shallow copy
- Deep Copy VS Shallow Copy
- Shallow Copy and Deep Copy
- shallow copy and deep copy
- Shallow Copy与Deep Copy
- ATM取款机系统模拟仿真
- Table
- 保护眼睛的好方法-适合上班族经常面对电脑的GGMM
- 从这里开始,
- 用JS脚本判断输入框不为空
- 一个C#中浅复制(shallow copy)/深复制(deep copy)的例子
- java实现简单扫雷
- 用eclipse开发的android程序如何打包成apk文件
- XBRL如何与Office紧密结合?
- EnumSet 使用(一)
- java 发送邮件
- 用libgdx时的常见问题一览
- HDU1224 Free DIY Tour
- 20.VC(ui)-图片处理的一些函数