zoukankan      html  css  js  c++  java
  • 关于Clone


    关于Clone的学习

    前阵子看到blog中关于Clone的资料

    整理下:
    关于浅表CLONE,用一个指针指向该地址。也就是说至少有2个指针指向这个地址。
    深度CLONE,就是新建一个,并复制原有的数据。克隆对象和原始对象地址肯定不一样。

    本人用以下代码测试:

    1    深度复制(Deep Copy)与浅表复制(Shadow Copy)的比较
    37
    38    //测试点:
    39    private void button2_Click(object sender, System.EventArgs e)
    40    {
    41        Person p = new Person();
    42        p.PersonName = new Name();
    43        p.PersonName.LastName = "jill";
    44        p.PersonName.FirstName = "zhang";
    45        p.Email = "jillzhang@126.com";
    46        Person sameNamePerson = p.Clone() as Person;
    47        sameNamePerson.ChangLastName("clr_");
    48        this.TestInfo.Text += "深度克隆原始数据:" + p.PersonName.LastName + "\r\n";
    49        this.TestInfo.Text += "深度克隆数据:" + sameNamePerson.PersonName.LastName + "\r\n";            
    50          
    51        this.TestInfo.Text += "浅表克隆原始数据:" + p.PersonName.LastName + "\r\n";
    52        Person samePerson = p.ReturnOne();
    53        samePerson.ChangLastName("Shadow");
    54
    55            
    56        this.TestInfo.Text += "浅表克隆数据:" + samePerson.PersonName.LastName + "\r\n";            
    57        this.TestInfo.Text += "深度克隆数据:" + sameNamePerson.PersonName.LastName + "\r\n";               
    58    }


    显示结果:
    深度克隆原始数据:jill
    深度克隆数据:clr_
    浅表克隆原始数据:jill
    浅表克隆数据:Shadow
    深度克隆数据:clr_

    特别说明的是:
    DataSet

    DataSet有个Clone和继承ICloneable后调用MemberwiseClone结果不一样,至于Copy属于浅表克隆还是深度克隆,暂时观察到结果。

    还是先看下代码吧:

     1private void button3_Click(object sender, System.EventArgs e)
     2{
     3    DataSet first = new DataSet();
     4    DataTable table = first.Tables.Add("table");
     5    table.Columns.Add("C1"typeof(int));
     6    table.Columns.Add("C2"typeof(char));
     7    table.PrimaryKey = new DataColumn[] { table.Columns[0] };
     8    for (int i = 0; i < 26++i)
     9    {
    10        table.Rows.Add(new object[] { i, (char)((ushort)'a' + i) });
    11    }

    12
    13    DataSet clone = first.Clone();
    14
    15    DataSet copy = first.Copy();   
    16        
    17    first.Dispose();
    18
    19    if (first == null)
    20        this.TestInfo.Text += "first has disponsed! \r\n";
    21
    22    this.TestInfo.Text += "Clone 结构,包括几张表:" + clone.Tables.Count.ToString() + "\r\n";
    23    if (clone.Tables.Count > 0)
    24    {
    25        this.TestInfo.Text += "表列还在么?" + clone.Tables[0].Columns.Count.ToString() + "\r\n";
    26        this.TestInfo.Text += "表数据还在么?行数:" + clone.Tables[0].Rows.Count.ToString() + "----我clone本身就没有数据\r\n";
    27    }

    28
    29    this.TestInfo.Text += "Copy 结构,包括几张表:" + copy.Tables.Count.ToString() + "\r\n";
    30    if (clone.Tables.Count > 0)
    31    {
    32        this.TestInfo.Text += "表列还在么?列数:" + copy.Tables[0].Columns.Count.ToString() + "\r\n";
    33        this.TestInfo.Text += "表数据还在么?行数:" + copy.Tables[0].Rows.Count.ToString() + "\r\n";
    34    }

    35
    36}

    37
    38

    测试结果:
    Clone 结构,包括几张表:1
    表列还在么?2
    表数据还在么?行数:0----我clone本身就没有数据
    Copy 结构,包括几张表:1
    表列还在么?列数:2
    表数据还在么?行数:26


    为了让浏览者理解原始的,我拷贝了MSDN的部分资料:
    [Begin]
    .NET on Clonation
    You find two kinds of types in .NET: value and reference. The former includes primitive types, enums and structs, and is allocated on the stack. The latter includes classes and arrays and goes to the heap. Basically, a value type carries all of its content with it. By contrast, a reference type points to a memory buffer where all, or part, of its content is maintained.

    In .NET you can duplicate objects in two ways that offer different functionality and power. You can create either a shallow or deep copy of a .NET object.

    A shallow copy is a copy of the object only. If the object has a complex structure and contains references to child objects, these won't be duplicated. Any of these child objects will refer to the original object in the shallow copy. (More or less what happens with the data, but not with bookmarks and filters, in a cloned ADO Recordsets.)

    A deep copy is a full copy of the object. You get a new object in which everything that is directly or indirectly referenced by the original object is duplicated.

    The root object of the .NET Framework, namely the Object object, has a method called MemberwiseClone that creates a shallow copy of the current object.

    protected object MemberwiseClone();

    This method represents the built-in capability of .NET objects to implement shallow copy. The method is protected and cannot be overridden.

    If you think that the standard shallow copy mechanism doesn't work for your objects, you either provide a brand new method to clone/copy them or you implement the ICloneable interface. If you want more than the standard clonation—a shallow copy as described earlier—some measure must be taken.

    [End]








  • 相关阅读:
    固态硬盘和机械硬盘的比较和SQLSERVER在两种硬盘上的性能差异
    带您理解SQLSERVER是如何执行一个查询的
    SQL Server 2000中的并行处理和执行计划中的位图运算符
    SQL2005解密已经被加密的存储过程
    使用SQLServer 2008的CDC功能实现数据变更捕获
    like语句百分号前置会使用到索引吗?
    再说一下表分区
    SQLSERVER中的元数据锁
    验证非法电话号码
    OpenCV 2.2版本号以上显示图片到 MFC 的 Picture Control 控件中
  • 原文地址:https://www.cnblogs.com/GoGoagg/p/1244308.html
Copyright © 2011-2022 走看看