■ ICloneable 인터페이스 : 객체 DEEP COPY 사용하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


School.cs

 

 

using System;

 

namespace TestProject

{

    /// <summary>

    /// 학교

    /// </summary>

    public class School : ICloneable

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region ID - ID

 

        /// <summary>

        /// ID

        /// </summary>

        public int ID { get; set; }

 

        #endregion

        #region 명칭 - Name

 

        /// <summary>

        /// 명칭

        /// </summary>

        public string Name { get; set; }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 객체 SHALLOW 복사하기 - ShallowCopy()

 

        /// <summary>

        /// 객체 SHALLOW 복사하기

        /// </summary>

        /// <returns>복사 객체</returns>

        public School ShallowCopy()

        {

            return (School)MemberwiseClone();

        }

 

        #endregion

        #region 복제하기 - Clone()

 

        /// <summary>

        /// 복제하기

        /// </summary>

        /// <returns>복제 객체</returns>

        public School Clone()

        {

            return DeepCopy();

        }

 

        #endregion

        #region 복제하기 - ICloneable.Clone()

 

        /// <summary>

        /// 복제하기

        /// </summary>

        /// <returns>복제 객체</returns>

        object ICloneable.Clone()

        {

            return Clone();

        }

 

        #endregion

 

        ////////////////////////////////////////////////////////////////////////////////////////// Protected

 

        #region 객체 DEEP 복사하기 - DeepCopy()

 

        /// <summary>

        /// 객체 DEEP 복사하기

        /// </summary>

        /// <returns>복사 객체</returns>

        protected virtual School DeepCopy()

        {

            School school = new School();

 

            school.ID   = ID;

            school.Name = string.Copy(Name);

 

            return school;

        }

 

        #endregion

    }

}

 

 

Student.cs

 

 

using System;

 

namespace TestProject

{

    /// <summary>

    /// 학생

    /// </summary>

    public class Student : ICloneable

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region ID - ID

 

        /// <summary>

        /// ID

        /// </summary>

        public int ID { get; set; }

 

        #endregion

        #region 명칭 - Name

 

        /// <summary>

        /// 명칭

        /// </summary>

        public string Name { get; set; }

 

        #endregion

        #region 학교 - School

 

        /// <summary>

        /// 학교

        /// </summary>

        public School School { get; set; }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 객체 SHALLOW 복사하기 - ShallowCopy()

 

        /// <summary>

        /// 객체 SHALLOW 복사하기

        /// </summary>

        /// <returns>복사 객체</returns>

        public Student ShallowCopy()

        {

            return (Student)MemberwiseClone();

        }

 

        #endregion

        #region 복제하기 - Clone()

 

        /// <summary>

        /// 복제하기

        /// </summary>

        /// <returns>복제 객체</returns>

        public Student Clone()

        {

            return DeepCopy();

        }

 

        #endregion

        #region 복제하기 - ICloneable.Clone()

 

        /// <summary>

        /// 복제하기

        /// </summary>

        /// <returns>복제 객체</returns>

        object ICloneable.Clone()

        {

            return Clone();

        }

 

        #endregion

 

        ////////////////////////////////////////////////////////////////////////////////////////// Protected

 

        #region 객체 DEEP 복사하기 - DeepCopy()

 

        /// <summary>

        /// 객체 DEEP 복사하기

        /// </summary>

        /// <returns>복사 객체</returns>

        protected virtual Student DeepCopy()

        {

            Student student = new Student();

 

            student.ID     = ID;

            student.Name   = string.Copy(Name);

            student.School = School.Clone();

 

            return student;

        }

 

        #endregion

    }

}

 

 

Program.cs

 

 

using System;

 

namespace TestProject

{

    /// <summary>

    /// 프로그램

    /// </summary>

    class Program

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region 프로그램 실행하기 - Main()

 

        /// <summary>

        /// 프로그램 실행하기

        /// </summary>

        private static void Main()

        {

            School school1 = new School();

 

            school1.ID   = 1;

            school1.Name = "동북고";

 

            Student student1 = new Student();

 

            student1.ID     = 1;

            student1.Name   = "홍길동";

            student1.School = school1;

 

            Student student2 = student1.Clone();

 

            Console.WriteLine(student1.School == student2.School);

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요