深拷贝和浅拷贝解析
1.在.NET 中,凡是实现了ICloneable接口的类型都具备了起对象实例的能力.
如:System.String,System.Array,System.DateSet….
2.所谓对象克隆就是对象拷贝
ICloneable接口的定义
Public interface ICloneable
{
object clone();
}
3.浅拷贝(Shallow Copy):指对象的字段被拷贝,而字段引用的对象不会被拷贝,拷贝的对象和源对象只是名称相同,但是他们共用一个实体。(提醒自己:大部分引用类型一般都是执行的浅拷贝哟)
/*
*功能:实现的浅拷贝
*Author:.NET RJ
*Time:09/9/09
*/
using System;
namespace Clone_ShallowCopyAndDeepCoyp
{
class Program
{
static void Main(string[] args)
{
Student stu = new Student("renjie", 21);
Student stu2 = stu;
stu2.Age = 22;
stu2.Show();
}
}
class Student
{
private string name;
private int age;
/// <summary>
/// name
/// </summary>
public string Name
{
get { return name; }
set { name = value; }
}
/// <summary>
/// age
/// </summary>
public int Age
{
get { return age; }
set { age = value; }
}
/// <summary>
///构造器
/// </summary>
/// <param name="name">name</param>
/// <param name="age">age</param>
public Student(string name, int age)
{
this.name = name;
this.age = age;
}
public void Show()
{
Console.WriteLine(name + "'s age is:" + age);
Console.ReadLine();
}
}
}
结果为:
stu=stu2 执行的是浅拷贝,stu和stu2对象执行的同一块内存地址,对stu2修改,同时也修改了stu的值。
深拷贝(Deep Copy):对象的字段被拷贝,同时字段引用的对象也进行拷贝,拷贝的对象和源对象是独立的(这个和浅拷贝的不同),修改一个对象不,另一个对象不会受到影响。(提醒自己:大部分值类型一般都是执行的深拷贝哟)
void Main()
{
int i=23;
int j=i;
j=200;
Console.WriteLine(i);
}
//结果为:23
j = i操作执行的是深拷贝,j的修改不会影响到i。
浅拷贝和深拷贝的实现
问题引用:怎样实现Clone方法的浅拷贝和深拷贝的定义呢?
通过一个System.Array 类的Clone方法来实现:
Public object Clone()
{
Return base.MemberwiseClone();
}
值类型是通过MemberwiseClone()来实现浅拷贝,那么我们也可以定义一个MemberwiseClone()方法来实现浅拷贝。(可以看出Clone方法是调用了基类的MemberwiseClone ())
/*
*功能:实现的浅拷贝
*Autor:.NET RJ
*Time:09/9/09
*/
using System;
using System.Collections.Generic;
namespace Clone_ShallowCopyAndDeepCoyp
{
class Program
{
static void Main(string[] args)
{
EnrollMent enrollmentlist = new EnrollMent();
enrollmentlist.students.Add(new Student("renjie", 21));
enrollmentlist.students.Add(new Student("NET RJ", 22));
//克隆的对象clonestudentlist
EnrollMent clonestudentlist = enrollmentlist.Clone() as EnrollMent;
enrollmentlist.ShowInfo();
clonestudentlist.ShowInfo();
//修改克隆的对象的值
clonestudentlist.students[0].Name = "老强";
clonestudentlist.students[0].Age = 30;
Console.WriteLine("-----------------------------");
enrollmentlist.ShowInfo();
clonestudentlist.ShowInfo();
}
}
class Student
{
private string name;
private int age;
/// <summary>
/// name
/// </summary>
public string Name
{
get { return name; }
set { name = value; }
}
/// <summary>
/// age
/// </summary>
public int Age
{
get { return age; }
set { age = value; }
}
/// <summary>
///构造器
/// </summary>
/// <param name="name">name</param>
/// <param name="age">age</param>
public Student(string name, int age)
{
this.name = name;
this.age = age;
}
public void Show()
{
Console.WriteLine(name + "'s age is:" + age);
Console.ReadLine();
}
}
/// <summary>
/// 学生登记类
/// </summary>
class EnrollMent : ICloneable
{
public List<Student> students = new List<Student>();
public void ShowInfo()
{
Console.WriteLine("Students enrollment information:");
foreach (Student stu in students)
{
Console.WriteLine("{0}'s age is:{1}", stu.Name, stu.Age);
}
}
#region ICloneable 成员
public object Clone()
{
//创建浅拷贝—执行
return MemberwiseClone();
}
#endregion
}
}
修改了克隆对象的值,同时也把源对象的值也修改了,所以它实现的是一个浅拷贝。