浅谈对象的深度拷贝 - IClonable接口

经常我们会发现,当我们把一个对象列表赋值给另一个对象列表之后,一个改变了,另一个

也跟着改变了,但是这往往不是我们想看到的

那么,怎么办呢?

办法只有一个,那就是让你的对象实现IClonable接口

对象代码:

public  class Employee : ICloneable
    {
         public  int EmployeeID {  getset; }
         public  string LastName {  getset; }
         public  string FirstName {  getset; }
         public  string Title {  getset; }
         public  string City {  getset; }
         public  string Region {  getset; }
         public  string Country {  getset; }
         public  string Notes {  getset; }

         public  override  string ToString()
        {
             string format =  " Employee ID: {0}\nFirst Name: {1}\n "
                          +  " Last Name: {2}\nTitle: {3}\nCity: {4}\n "
                          +  " Region: {5}\nCountry: {6}\nNotes: {7}\n ";

             return  string.Format(format, EmployeeID, FirstName, LastName, Title, City, Region, Country, Notes);
        }

        
         public Object Clone()
        {  
            Employee cloned =  new Employee();

            cloned.EmployeeID =  this.EmployeeID;
            cloned.LastName =  this.LastName;
            cloned.FirstName =  this.FirstName;
            cloned.Title =  this.Title;
            cloned.City =  this.City;
            cloned.Region =  this.Region;
            cloned.Country =  this.Country;
            cloned.Notes =  this.Notes;

             return cloned;  
        }
    }

 

测试代码如下:

public  void Run()
        {
            EmployeesClient employeeClient =  new EmployeesClient();
            List<Employee> srcEmployeeList = employeeClient.GetAllEmployees();

            Console.WriteLine( " Source Employee List: ");
            Console.WriteLine( " -------------------------------------------------------------------- ");
            Display(srcEmployeeList);
            Console.WriteLine( " -------------------------------------------------------------------- ");

            Console.WriteLine();
            Console.WriteLine();

            List<Employee> dstEmployeeList =  new List<Employee>();
             srcEmployeeList.ForEach(emp => dstEmployeeList.Add( (Employee)emp.Clone() ));
            
            srcEmployeeList[ 0].LastName =  " Huang ";
            srcEmployeeList[ 0].FirstName =  " Lynn ";

            Console.WriteLine( " Source Employee List After Change: ");
            Console.WriteLine( " -------------------------------------------------------------------- ");
            Display(srcEmployeeList);
            Console.WriteLine( " -------------------------------------------------------------------- ");

            Console.WriteLine();
            Console.WriteLine();

            Console.WriteLine( " Dest Employee List: ");
            Console.WriteLine( " -------------------------------------------------------------------- ");
            Display(dstEmployeeList);
            Console.WriteLine( " -------------------------------------------------------------------- ");
        }

         private  void Display(IList<Employee> employeeList)
        {
             foreach (Employee employee  in employeeList)
            {
                Console.WriteLine(employee);
            }
        }

 

运行结果:

 

Source Employee List After Change:
--------------------------------------------------------------------
Employee ID: 1
First Name: Lynn
Last Name: Huang

......

 

 

Dest Employee List:
--------------------------------------------------------------------
Employee ID: 1
First Name: Nancy
Last Name: Davolio

......

 

 

转载于:https://www.cnblogs.com/davidgu/archive/2012/05/31/2528836.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值