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

本文共 2727 字,大约阅读时间需要 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

......

 

 

转载地址:http://fipfm.baihongyu.com/

你可能感兴趣的文章
jQuery上注册函数的方法
查看>>
不要将@Autowired注解用于static方法
查看>>
关于达内培训的名企定制班
查看>>
Routing with restify and socket.io in node
查看>>
立体测距
查看>>
关于离线下载的一些免费的网站
查看>>
开发netfilter的一些坑
查看>>
java中map的clear和new性能对比
查看>>
macbook 备份系统
查看>>
klish 安装与使用
查看>>
Django实战(18):提交订单
查看>>
PHP时间戳函数总结
查看>>
开发自定义JSF组件(1) HelloWorld
查看>>
设计模式学习——策略模式:模拟鸭子应用
查看>>
lucene4.7 分词器(三)
查看>>
linux下让tomcat以service方式运行(及使用service tomcat start)
查看>>
HTML模版
查看>>
观察者模式摘要
查看>>
我的友情链接
查看>>
xml中单双引号
查看>>