码迷,mamicode.com
首页 > 其他好文 > 详细

深复制和浅复制

时间:2014-12-25 15:57:30      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

深复制和浅复制的区别在于引用的复制,值类型系统会自动完成,而且完成的很好
using
System; public class IdInfo { public int IdNumber = 2; } public class Person : ICloneable { public int Age; public string Name; public IdInfo IdInfo; public Person() { IdInfo = new IdInfo(); } public Person ShallowCopy()//浅赋值也成为影子拷贝 { return (Person)(MemberwiseClone()); } public object Clone()//深复制 { //Person other=new Person()等效的 //return other; return new Person(); } } public class Example { public static void Main() { Person p1 = new Person(); p1.Age = 11; p1.Name = "lei"; Person p2 = p1.ShallowCopy(); Person p3 = (Person)p1.Clone(); Console.WriteLine("Original values of p1 and p2,p3:"); Console.WriteLine(" p1 instance values: "); DisplayValues(p1); Console.WriteLine(" p2 instance values:"); DisplayValues(p2); Console.WriteLine(" p3 instance values: "); DisplayValues(p3); Console.ReadLine(); } public static void DisplayValues(Person p) { Console.WriteLine(" Name: {0:s}, Age: {1:d}", p.Name, p.Age); Console.WriteLine(" Value: {0:d}", p.IdInfo.IdNumber); } }
技术分享
为什么p3的值是空的,注意看clone这个深复制函数,它返回一个局部变量,而这个局部变量的值是未知的,输出的值是系统初始化的值,string型初始化为“”,int型初始化为0!
using System;
public class IdInfo
{
    public int IdNumber = 2;

}

public class Person : ICloneable
{
    public int Age;
    public string Name;
    public IdInfo IdInfo;
    public Person()
    { IdInfo = new IdInfo(); }
    public Person ShallowCopy()//浅赋值也成为影子拷贝
    {
        return (Person)(MemberwiseClone());
    }

    public object Clone()//深复制
    {
        Person other;
        other=(Person)this.MemberwiseClone();//语句1
语句1执行完后,局部变量other的值类型成员有了自己的空间,但是引用类型成员IdInfo指向的是this所指的空间,也就是说other和this所指的变量共同指向同一个空间 other.IdInfo = new IdInfo();//语句2 这句执行后other的引用类型的成员IdInfo有了属于自己的空间 return other; } } //语句1和语句2的顺序不能颠倒,不然会发生如下错误
技术分享
public class Example { public static void Main() { Person p1 = new Person(); p1.Age = 11; p1.Name = "lei"; Person p2 = p1.ShallowCopy(); Person p3 = (Person)p1.Clone(); Console.WriteLine("Original values of p1 and p2,p3:"); Console.WriteLine(" p1 instance values: "); DisplayValues(p1); Console.WriteLine(" p2 instance values:"); DisplayValues(p2); Console.WriteLine(" p3 instance values: "); DisplayValues(p3); // Change the value of p1 properties and display the values of p1 and p2,p3. p1.Age = 12; //p1改变观看p2,p3的变化; p1.Name = "cao"; p1.IdInfo.IdNumber = 3; p3.IdInfo.IdNumber = 4;//p3的值改变 Console.WriteLine("\nValues of p1 and p2 after changes to p1:"); Console.WriteLine(" p1 instance values: "); DisplayValues(p1); Console.WriteLine(" p2 instance values:"); DisplayValues(p2); Console.WriteLine(" p3 instance values: "); DisplayValues(p3); Console.ReadLine(); } public static void DisplayValues(Person p) { Console.WriteLine(" Name: {0:s}, Age: {1:d}", p.Name, p.Age); Console.WriteLine(" Value: {0:d}", p.IdInfo.IdNumber); } }


下面是msn中的:

using System;

public class IdInfo
{
    public int IdNumber;

    public IdInfo(int IdNumber)
    {
        this.IdNumber = IdNumber;
    }
}

public class Person 
{
    public int Age;
    public string Name;
    public IdInfo IdInfo;

    public Person ShallowCopy()
    {
       return (Person)this.MemberwiseClone();
    }

    public Person DeepCopy()
    {
       Person other = (Person) this.MemberwiseClone(); 
       other.IdInfo = new IdInfo(this.IdInfo.IdNumber);
       return other;
    }
}

public class Example
{
    public static void Main()
    {
        // Create an instance of Person and assign values to its fields.
        Person p1 = new Person();
        p1.Age = 42;
        p1.Name = "Sam";
        p1.IdInfo = new IdInfo(6565);

        // Perform a shallow copy of p1 and assign it to p2.
        Person p2 = (Person) p1.ShallowCopy();

        // Display values of p1, p2
        Console.WriteLine("Original values of p1 and p2:");
        Console.WriteLine("   p1 instance values: ");
        DisplayValues(p1);
        Console.WriteLine("   p2 instance values:");
        DisplayValues(p2);

        // Change the value of p1 properties and display the values of p1 and p2.
        p1.Age = 32;
        p1.Name = "Frank";
        p1.IdInfo.IdNumber = 7878;
        Console.WriteLine("\nValues of p1 and p2 after changes to p1:");
        Console.WriteLine("   p1 instance values: ");
        DisplayValues(p1);
        Console.WriteLine("   p2 instance values:");
        DisplayValues(p2);

        // Make a deep copy of p1 and assign it to p3.
        Person p3 = p1.DeepCopy();
        // Change the members of the p1 class to new values to show the deep copy.
        p1.Name = "George";
        p1.Age = 39;
        p1.IdInfo.IdNumber = 8641;
        Console.WriteLine("\nValues of p1 and p3 after changes to p1:");
        Console.WriteLine("   p1 instance values: ");
        DisplayValues(p1);
        Console.WriteLine("   p3 instance values:");
        DisplayValues(p3);
    }

    public static void DisplayValues(Person p)
    {
        Console.WriteLine("      Name: {0:s}, Age: {1:d}", p.Name, p.Age);
        Console.WriteLine("      Value: {0:d}", p.IdInfo.IdNumber);
    }
}
http://msdn.microsoft.com/zh-cn/library/system.object.memberwiseclone(v=vs.110).aspx

 

ICloneable 接口
支持克隆,即用与现有实例相同的值创建类的新实例。
public interface ICloneable
ICloneable 类型公开以下成员。
ICloneable.Clone 方法
创建作为当前实例副本的新对象。
Object Clone()
返回值
类型:System.Object
作为此实例副本的新对象。

Object.MemberwiseClone 方法
创建当前 Object 的浅表副本。
protected Object MemberwiseClone()
返回值
类型:System.Object
当前 Object 的浅表副

 

 

 

 

深复制和浅复制

标签:

原文地址:http://www.cnblogs.com/leijiangtao/p/4184678.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!