ITPub博客

首页 > Linux操作系统 > Linux操作系统 > ICloneable.Clone()--------克隆

ICloneable.Clone()--------克隆

原创 Linux操作系统 作者:iDotNetSpace 时间:2008-09-27 13:46:37 0 删除 编辑

克隆是创建作为当前实例副本的新对象。

克隆分为深度克隆和浅度克隆

深度克隆:会克隆当前实例的所有所有成员.

浅度克隆:只会克隆当前实例的所有值类型的.


浅度克隆Object类为我们提供了一个受保护的克隆方法MemberwiseClone()

深度克隆要我们自己实现

实现深度克隆的方式一般有2种... (我只知道2种... 如果你知道另外几种,请回复告诉我..)

我来创建2个类

Address类  有2个属性 Province 和City

 

 


<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--> [Serializable]//这个标记是表示可以序列化这个类
    public class Address
    {
        
private string _city;

        
public string City
        {
            
get { return _city; }
            
set { _city = value; }
        }

        
private string _province;

        
public string Province
        {
            
get { return _province; }
            
set { _province = value; }
        }
    

    }

 

Person 类有3个属性  Name 、Age、Address 

 


<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->    [Serializable]
    
public class Person
    {
        
private string _name;
        
private int _age;
        
private Address _address;


        
public Person(string name,int age,Address address)
        {
            _name 
= name;
            _age 
= age;
            _address 
= address;
        }

        
public Person()
        {

        }

        
public Address Address
        {
            
get { return _address; }
            
set { _address = value; }
        }

        
public string Name
        {
            
get { return this._name; }
            
set {this._name = value;}
        }



        
public int Age
        {
            
get { return _age; }
            
set { _age = value; }
        }


    }

 

我们现在来让Person实现克隆

在Person类中加入

 

 


<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--> public Person Clone()
        {
            MemoryStream ms 
= new MemoryStream();
            BinaryFormatter bf 
= new BinaryFormatter();
            ms.Seek(
0, SeekOrigin.Begin);
            bf.Serialize(ms, 
this);
            ms.Seek(
0, SeekOrigin.Begin);
            
return (Person)bf.Deserialize(ms);
            
        }

这个方法就可以实现克隆了 

 这个方法是利用序列化和反序列化来实现克隆 比较方便但是类必须用[Serializable]标记可以序列化

令一种方式


<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->public Person Clone()
        {
            Person temp 
= new Person();
            temp.Name 
= this.Name;
            temp.Age 
= this.Age;
            temp.Address.Province 
= this.Address.Province;
            temp.Address.City 
= this.Address.City;
            
return temp;
            
        }

 这种方式容易出错 当一个类的成员过多时容易出错。当你要修改类的成员时,这个克隆方法也要修改 。

建议用第序列化的方式来实现克隆。

浅度克隆就很简单了 调用受保护的克隆方法MemberwiseClone()就可以了

 


<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->public Person Clone()
        {
            
return this.MemberwiseClone();
            
        }

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-465900/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-01-04

  • 博文量
    2376
  • 访问量
    5765920