一、关于深克隆和浅克隆的实现
Object.MemberwiseClone( )只能实现对象的浅克隆,我们可以实现ICloneable接口的Clone( )来实现对象的深克隆和浅克隆,示例如下:
<1>浅克隆
using System;
public class Person : ICloneable
{
public object Clone()
{
return this;
}
public string Name { get; set; }
public string Sex { get; set; }
}
public class Person : ICloneable
{
public object Clone()
{
return this;
}
public string Name { get; set; }
public string Sex { get; set; }
}
<2>深克隆
using System;
public class Person : ICloneable
{
public object Clone()
{
return this.MemberwiseClone();
}
public string Name { get; set; }
public string Sex { get; set; }
}
public class Person : ICloneable
{
public object Clone()
{
return this.MemberwiseClone();
}
public string Name { get; set; }
public string Sex { get; set; }
}
浙公网安备 33010602011771号