博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
浅谈对象的深度拷贝 - IClonable接口
阅读量:7294 次
发布时间:2019-06-30

本文共 2795 字,大约阅读时间需要 9 分钟。

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

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

那么,怎么办呢?

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

对象代码:

public 
class Employee : ICloneable
    {
        
public 
int EmployeeID { 
get
set; }
        
public 
string LastName { 
get
set; }
        
public 
string FirstName { 
get
set; }
        
public 
string Title { 
get
set; }
        
public 
string City { 
get
set; }
        
public 
string Region { 
get
set; }
        
public 
string Country { 
get
set; }
        
public 
string Notes { 
get
set; }
        
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

你可能感兴趣的文章
JSP标签JSTL(4)--URL
查看>>
DiscuX END - 553 Envolope sender mismatch with header from..
查看>>
论文笔记之:Instance-aware Semantic Segmentation via Multi-task Network Cascades
查看>>
单例模式
查看>>
你能用微信小程序打开小程序了【附开发方法】
查看>>
【BOOM】一款有趣的Javascript动画效果
查看>>
Osmocom-bb系统编译
查看>>
SQL Server-聚焦深入理解动态SQL查询(三十二)
查看>>
高考查分数微信就能搞定
查看>>
ORM简介
查看>>
so 问题来了,你现在值多少钱?
查看>>
17.3. mpstat
查看>>
dataguard中MRP无法启动的问题分析和解决
查看>>
Oracle 12C R2-新特性-转换函数的增强
查看>>
ITIL的一些简单感受
查看>>
使用oracheck进行系统巡检
查看>>
云计算+物联网的前景更加诱人
查看>>
SQL Server 中的事务与事务隔离级别以及如何理解脏读, 未提交读,不可重复读和幻读产生的过程和原因...
查看>>
购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(2)--前端,以及前后端Session
查看>>
HDOJ/HDU 2566 统计硬币(公式~遍历~)
查看>>