Object之MemberwiseClone方法

示例:

代码
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.Web.Profile;

namespace WebApplication1
{
    
public partial class _Default : System.Web.UI.Page
    {
        
protected void Page_Load(object sender, EventArgs e)
        {
            Employee em1 
= new Employee(5678);
            Employee em2 
= (Employee)em1.Clone();
            em1.First 
= "san";
            em2.Last 
= "zhang";
            Response.Write(
"HC:"+em1.GetHashCode());
            Response.Write(
"<br/>");
            Response.Write(em1.EmplID
+":"+em1.FullName);
            Response.Write(
"<br/>");
            Response.Write(
"HC:"+em2.GetHashCode());
            Response.Write(
"<br/>");
            Response.Write(em2.EmplID
+":"+em2.FullName);
            Response.Write(
"<br/>");
        }

        
public class Employee : ICloneable
        {
            
public Employee(int id)
            {
                
if ((id < 1000|| (id > 9999))
                {
                    
throw new Exception(
                        
"Invalid Employee ID");
                }

                propID 
= id;
            }

            
public object Clone()
            {
                
return MemberwiseClone();
            }


            
private readonly int propID;
            
public int EmplID
            {
                
get
                {
                    
return propID;
                }
            }

            
public override string ToString()
            {
                
return FullName;
            }

            
public class Name
            {
                
string firstName;

                
public string FirstName
                {
                    
get { return firstName; }
                    
set { firstName = value; }
                }
                
string lastName;

                
public string LastName
                {
                    
get { return lastName; }
                    
set { lastName = value; }
                }

            }

            Name empName 
= new Name();

         

            
public string First
            {
                
get
                {
                    
return empName.FirstName;
                }

                
set
                {
                    empName.FirstName 
= value;
                }
            }

            
public string Last
            {
                
get
                {
                    
return empName.LastName;
                }

                
set
                {
                    empName.LastName 
= value;
                }
            }

            
public string FullName
            {
                
get { return First + Last; }
            }

        }

    }

}

 

 

posted @ 2010-12-19 22:10  唔愛吃蘋果  阅读(1398)  评论(0编辑  收藏  举报