>  기사  >  백엔드 개발  >  .NET Framework에서 복제를 얕은 복제에서 깊은 복제로 변경하는 방법에 대한 자세한 샘플 코드

.NET Framework에서 복제를 얕은 복제에서 깊은 복제로 변경하는 방법에 대한 자세한 샘플 코드

黄舟
黄舟원래의
2017-03-18 13:20:081216검색

어떤 경우에는 문제를 해결하기 위해 얕은 복사본이 필요합니다. 왜냐하면 복사한 인스턴스가 여전히 원래 초기 객체참조하기 때문입니다. 그러나 때로는 이것만으로는 충분하지 않습니다. 왜냐하면 우리가 복사한 인스턴스는 참조 유형의 로컬 값을 수정 및 조정해야 하고 원본 객체에 영향을 미치지 않도록 해야 하기 때문입니다!

딥 카피가 필요합니다!

요구 사항은 다음과 같습니다.
먼저 얕은 사본이 우리의 요구 사항을 충족할 수 없는 이유를 살펴보겠습니다. 이력서를 복사하려고 하는데 복사된 버전이 회사 이름과만 일치하지 않습니다. 템플릿 이력서의 입사 지원 의도.

코드의 첫 번째 버전은 다음과 같습니다.

ResumeModelVersion 1.0

    public class ResumeInfo1:ICloneable
    {        
    public ResumeInfo1(string name, string telephone, EducationInfo educationInfo,WantedJob1 job)
        {            
        this._name = name;            
        this._telephone = telephone;            
        this._educationInfo = educationInfo;            
        this._wantedJob = job;
        }        
        private string _name;        
        public string name
        {            
        get { return this._name; }
        }       
         private string _telephone;        
         public string telephone
        {            
        get { return _telephone; }
        }        
        private EducationInfo _educationInfo;        
        public EducationInfo educationInfo
        {            
        get { return _educationInfo; }
        }        
        private WantedJob1 _wantedJob;        
        public WantedJob1 WantedJob
        {            
        get { return _wantedJob; }
        }        
        public object Clone()
        {           
        return this.MemberwiseClone();
        }


    }

중첩된 하위 클래스 교육 배경 객체 EducationInfo

    public class EducationInfo
    {        
    public string schoolName { get; set; }        
    public DateTime enrollTime { get; set; }        
    public DateTime leaveTime { get; set; }
    }

내포된 객체인 WantedJob1:

    public class WantedJob1
    {        
    public string companyName { get; set; }        
    public double eanrings { get; set; }

    }

클라이언트에서 사용합니다:

            EducationInfo educationInfo = new EducationInfo();
            WantedJob1 wantedJob = new WantedJob1();
            ResumeInfo1 templateResume = new ResumeInfo1("qaz", "18810002000", educationInfo, wantedJob);
            educationInfo.enrollTime = new DateTime(2010, 7, 1);
            educationInfo.leaveTime = new DateTime(2015, 1, 1);
            educationInfo.schoolName = "wsx";
            wantedJob1.companyName = "edc";
            wantedJob1.eanrings = 1000;            
            //假定各个简历版本,仅仅companyName属性值不同。

            var res1 = templateResume.Clone();
            (res1 as ResumeInfo1).WantedJob.companyName = "baidu";            
            var res2 = templateResume.Clone();
            (res1 as ResumeInfo1).WantedJob.companyName = "ali";

하지만 우리가 얻는 회사 이름은 모두 "ali"입니다

이것은 전형적인 얕은 복사본입니다!

은 회사명 구별 요건을 충족하지 못합니다. 계속해서 수정하고 딥카피로 변경하세요:

    public class ResumeInfo2:ICloneable
    {        
    public ResumeInfo2(string name, string telephone, EducationInfo educationInfo,WantedJob2 job)
        {            
        this._name = name;            
        this._telephone = telephone;            
        this._educationInfo = educationInfo;            
        this._wantedJob = job;
        }        //
        private void cloneJob(WantedJob2 job)
        {            
        this._wantedJob = job.Clone() as WantedJob2;
        }        private string _name;        
        public string name
        {            
        get { return this._name; }
        }        
        private string _telephone;        
        public string 
        telephone
        {            
        get { return _telephone; }
        }        
        private EducationInfo _educationInfo;        
        public EducationInfo educationInfo
        {            
        get { return _educationInfo; }
        }        
        private WantedJob2 _wantedJob;        
        public WantedJob2 WantedJob
        {            
        get { return _wantedJob; }
        }        
        public object Clone()
        {
            cloneJob(this._wantedJob);            
            return new ResumeInfo2(_name,_telephone,_educationInfo,_wantedJob);          
        }


    }

구직대상 2.0:

   //WantedJob2 实现接口
    public class WantedJob2:ICloneable
    {        
    public string companyName { get; set; }        
    public double eanrings { get; set; }        
    public object Clone()
        {            
        return this.MemberwiseClone();
        }
    }

고객전화:

rree

다른 회사 이름을 얻으세요! 전체 복사에 성공했습니다!

위 내용은 .NET Framework에서 복제를 얕은 복제에서 깊은 복제로 변경하는 방법에 대한 자세한 샘플 코드의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.