天生舞男

我喜欢谦虚的学习各种...,希望自己能坚持一辈子,因为即使一张卫生巾也是有它的作用.
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

About the deeply copy

Posted on 2006-03-23 15:24  天生舞男  阅读(361)  评论(0编辑  收藏  举报

In our program, we want to copy a object fully, and the object is nested, so when we design the class can using the copy constructor, there is a sample, you can run the code with CLR:

using
System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebApplicationTest
{
 
/// <summary>
  /// Summary description for TestDeepCopy.
  /// </summary>
 
public class TestDeepCopy: System.Web.UI.Page
  {
   
private void Page_Load(object sender, System.EventArgs e)
    {
     
ClassLevel1 objClassLevel1 = new ClassLevel1();
     
objClassLevel1.objClassLevel2 = new ClassLevel2();
     
objClassLevel1.objClassLevel2.objClassLevel3 = new ClassLevel3();
     
objClassLevel1.objClassLevel2.strClassLevel2 = "I am a string in level2 class";
      
      // using the instance of the ClassLevel1 to call
      // the copy constructor of ClassLevel1 class
     
ClassLevel1 copyObjClassLevel1 = new ClassLevel1(objClassLevel1);
     
Response.Write(copyObjClassLevel1.objClassLevel2.strClassLevel2);
    }

   
#region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
     
//
      // CODEGEN: This call is required by the ASP.NET Web Form Designer.
      //
     
InitializeComponent();
     
base.OnInit(e);
    }

   
/// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
   
private void InitializeComponent()
    {
     
this.Load += new System.EventHandler(this.Page_Load);
    }
   
#endregion
 
}

 
public class ClassLevel3
  {
    // Member variable
   
public string strClassLevel3;
    
    // Copy constructor
   
public ClassLevel3(ClassLevel3 objClassLevel3)
    {
     
this.strClassLevel3 = objClassLevel3.strClassLevel3;
    }
    
    // Default constructor
   
public ClassLevel3(){}
  }

 
public class ClassLevel2
  {
    // Member variable
   
public string strClassLevel2;
   
public ClassLevel3 objClassLevel3;
    
    // Copy constructor
   
public ClassLevel2(ClassLevel2 ojbClassLevel2)
    {
     
this.strClassLevel2 = ojbClassLevel2.strClassLevel2;
     
this.objClassLevel3 = new ClassLevel3(ojbClassLevel2.objClassLevel3);
    }
    
    // Default constrcutor
   
public ClassLevel2(){}
  }

 
public class ClassLevel1
  {
    // Member variable
   
public string strClassLevel1;
   
public ClassLevel2 objClassLevel2;
    
    // Copy constructor 
   
public ClassLevel1(ClassLevel1 ojbClassLevel1)
    {
     
this.strClassLevel1 = ojbClassLevel1.strClassLevel1;

      //Call the Copy constructor of ClassLevel2
     
this.objClassLevel2 = new ClassLevel2(ojbClassLevel1.objClassLevel2);
    }
    
    // Default constructor 
   
public ClassLevel1(){}
  }
}