- 1 using System;
- 2 using System.Collections.Generic;
- 3 using System.Linq;
- 4 using System.Text;
- 5 using System.Runtime.Serialization.Formatters.Binary; //序列化
- 6 using System.IO;//文件
- 7 using System.Runtime.Serialization;//序列化异常处理
- 8
- 9 namespace Customer
- 10 {
- 11 [Serializable]//将Customer类设置为可序列化
- 12 public abstract class Customer : ICloneable
- 13 {
- 14 public abstract object Clone();
- 15 }
- 16
- 17 [Serializable]//将Address类设置为可序列化
- 18 public class Address//地址类
- 19 {
- 20 private string province;
- 21 private string city;
- 22
- 23 public string City
- 24 {
- 25 get { return city; }
- 26 set { city = value; }
- 27 }
- 28
- 29 public string Province
- 30 {
- 31 get { return province; }
- 32 set { province = value; }
- 33 }
- 34
- 35 public Address(string province,string city)
- 36 {
- 37 this.province = province;
- 38 this.city = city;
- 39 }
- 40
- 41 public override string ToString()//打印地区
- 42 {
- 43 return "地址为:" + province + " 省," + city + " 市。";
- 44 }
- 45 }
- 46
- 47 [Serializable]//将CustomerA设置为可序列化
- 48 public class CustomerA : Customer//顾客A类
- 49 {
- 50 private string name;
- 51 private int age;
- 52 private string call;
- 53 private Address address;
- 54
- 55 public Address Address
- 56 {
- 57 get { return address; }
- 58 set { address = value; }
- 59 }
- 60
- 61 public string Name
- 62 {
- 63 get { return name; }
- 64 set { name = value; }
- 65 }
- 66
- 67 public int Age
- 68 {
- 69 get { return age; }
- 70 set { age = value; }
- 71 }
- 72
- 73 public string Call
- 74 {
- 75 get { return call; }
- 76 set { call = value; }
- 77 }
- 78
- 79 public CustomerA(string name, int age, string call,Address address)
- 80 {
- 81 this.name = name;
- 82 this.age = age;
- 83 this.call = call;
- 84 this.address = address;
- 85 }
- 86
- 87 public override string ToString()
- 88 {
- 89 return "客户A--姓名:" + this.name + " 年龄:" + this.age + " 联系方式:" + this.call + " "+ this.address.ToString();
- 90 }
- 91
- 92 #region 浅克隆+this.MemberwiseClone()
- 93 public object MemClone()
- 94 {
- 95 return this.MemberwiseClone();
- 96 }
- 97 #endregion
- 98
- 99 #region 深克隆+object Clone()
- 100 public override object Clone()
- 101 {
- 102 Kits.FileSer(@"d:\1.txt", this);//
- 103 object obj = Kits.FileDSer(@"d:\1.txt");
- 104 return obj;
- 105 }
- 106 #endregion
- 107 }
- 108
- 109 [Serializable]//将CustomerB设置为可序列化
- 110 public class CustomerB : Customer//顾客B类
- 111 {
- 112 private string name;
- 113 private int age;
- 114 private string call;
- 115 private Address address;
- 116
- 117 public Address Address
- 118 {
- 119 get { return address; }
- 120 set { address = value; }
- 121 }
- 122
- 123 public string Name
- 124 {
- 125 get { return name; }
- 126 set { name = value; }
- 127 }
- 128
- 129 public int Age
- 130 {
- 131 get { return age; }
- 132 set { age = value; }
- 133 }
- 134
- 135 public string Call
- 136 {
- 137 get { return call; }
- 138 set { call = value; }
- 139 }
- 140
- 141 public CustomerB(string name, int age, string call, Address address)
- 142 {
- 143 this.name = name;
- 144 this.age = age;
- 145 this.call = call;
- 146 this.address = address;
- 147 }
- 148
- 149 public override string ToString()
- 150 {
- 151 return "客户B--姓名:" + this.name + " 年龄:" + this.age + " 联系方式:" + this.call + " " + this.address.ToString();
- 152 }
- 153
- 154 #region 浅克隆+this.MemberwiseClone()
- 155 public object MemClone()
- 156 {
- 157 return this.MemberwiseClone();
- 158 }
- 159 #endregion
- 160
- 161 #region 深克隆+object Clone()
- 162 public override object Clone()
- 163 {
- 164 Kits.FileSer(@"d:\1.txt", this);//读
- 165 object obj = Kits.FileDSer(@"d:\1.txt");//写
- 166 return obj;
- 167 }
- 168 #endregion
- 169 }
- 170
- 171 public class Kits//工具类
- 172 {
- 173 public static void FileSer(string path,object obj)//读出信息
- 174 {
- 175 FileStream fs = new FileStream(path, FileMode.OpenOrCreate);//通道 序列化
- 176 BinaryFormatter bf = new BinaryFormatter();//搬运工
- 177 try
- 178 {
- 179 bf.Serialize(fs, obj);//序列化
- 180 }
- 181 catch (SerializationException e)
- 182 {
- 183 Console.WriteLine("该文件进行序列化失败。原因 : " + e.Message);
- 184 throw;
- 185
- 186 }
- 187 finally { fs.Close(); }
- 188 }
- 189
- 190 public static object FileDSer(string path)//写入
- 191 {
- 192 FileStream fs = new FileStream(path, FileMode.Open);//通道、路径,权限
- 193 BinaryFormatter bf = new BinaryFormatter();//搬运工
- 194 object obj = null;
- 195 try
- 196 {
- 197 obj=bf.Deserialize(fs);//反序列化
- 198 }
- 199 catch (SerializationException e)
- 200 {
- 201 Console.WriteLine("该文件进行反序列化失败。原因 : " + e.Message);
- 202 throw;
- 203
- 204 }
- 205 finally
- 206 {
- 207 fs.Close();
- 208 }
- 209 return obj;
- 210 }
- 211 }
- 212
- 213 class Program
- 214 {
- 215 static void Main(string[] args)
- 216 {
- 217 Console.WriteLine("\n--------------------------------Customer-------------------------------------");
- 218 Address addr1 = new Address("中国江苏", "扬州");
- 219 CustomerA c1 = new CustomerA("社会人c1", 20, "13288888888", addr1);
- 220 Console.WriteLine(c1.ToString());//c1的作为原对象
- 221
- 222 Console.WriteLine("\n-----------------------------------Copy(c2 = c1)-------------------------------------");
- 223 CustomerA c2 = c1;
- 224 Console.WriteLine("社会人c2,更新信息.直接复制对象\n");
- 225 Console.WriteLine();
- 226 Console.WriteLine(c2.ToString());
- 227
- 228 Console.WriteLine("\n-----------------------------ShallowCopy-------------------------------------");
- 229 Console.WriteLine();
- 230 Console.WriteLine("社会人c3,更新信息\n");
- 231 CustomerA c3 = (CustomerA)c1.MemClone();//浅克隆
- 232 Console.WriteLine(c3.ToString());
- 233
- 234 Console.WriteLine("此时 c2:");
- 235 Console.WriteLine(c2.ToString());
- 236
- 237 Console.WriteLine("\n--------------------------------Customer-------------------------------------\n");
- 238 Console.WriteLine();
- 239 Address addr2 = new Address("中国广东", "广州");
- 240 CustomerB c4 = new CustomerB("小猪佩琪", 24, "16612345678", addr2);
- 241 Console.WriteLine("c4 "+c4.ToString());
- 242 Console.WriteLine("\n--------------------------------DeepCopy(update c5.Age = 26)--------------------------\n");
- 243
- 244 CustomerB c5 = (CustomerB)c4.Clone();
- 245 c5.Age = 26;
- 246 Console.WriteLine("一年后,搬家\n");
- 247 c5.Address = new Address("中国天津", "河西区");
- 248 Console.WriteLine(c5.ToString());
- 249
- 250 Console.WriteLine("此时 c4:");
- 251 Console.WriteLine(c4.ToString());
- 252 ;
- 253 }
- 254 }
- 255 }