C# Class2

Chan·2021년 12월 2일
0

C#

목록 보기
4/10

객체 복사 ShallowCopy, DeepCopy

  • 객체 복사할 때 참조만 복사하는 것은 얕은 복사(ShallowCopy)

  • 예시 코드

//----Class1.cs----//
using System;
namespace ConsoleApp1202
{
    class MyClass
    {
        public int MyField1;
        public int MyField2;
    }
}

//----Program.cs----//
using System;
using static System.Console;

namespace ConsoleApp1202
{
    public class Program
    {
        static void Main(string[] args)
        {
            MyClass source = new MyClass();
            source.MyField1 = 10;
            source.MyField2 = 20;

            MyClass target = source; // ShallowCopy, 참조만 복사함
            target.MyField2 = 30;

            System.Console.WriteLine("{0} {1}", source.MyField1, source.MyField2); // 10 30
            System.Console.WriteLine("{0} {1}", target.MyField1, target.MyField2); // 10 30 
        }
    }
}



  • 깊은 복사(DeepCopy)는 힙에 보관되어 있는 내용을 source로부터 복사해서 받아 별도의 힙 공간에 객체를 보관하는 것

  • 예시 코드

using System;

namespace DeepCopy
{
    class MyClass
    {
        public int MyField1;
        public int MyField2;

        public MyClass DeepCopy()
        {
            MyClass newCopy = new MyClass();
            newCopy.MyField1 = this.MyField1;
            newCopy.MyField2 = this.MyField2;

            return newCopy;
        }
    }

    class MainApp
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Shallow Copy");

            {
                MyClass source = new MyClass();
                source.MyField1 = 10;
                source.MyField2 = 20;

                MyClass target = source; // ShallowCopy
                target.MyField2 = 30;

                Console.WriteLine($"{source.MyField1} {source.MyField2}"); // 10 30
                Console.WriteLine($"{target.MyField1} {target.MyField2}"); // 10 30
            }

            Console.WriteLine("Deep Copy");

            {
                MyClass source = new MyClass();
                source.MyField1 = 10;
                source.MyField2 = 20;

                MyClass target = source.DeepCopy(); // DeepCopy
                target.MyField2 = 30;

                Console.WriteLine($"{source.MyField1} {source.MyField2}"); // 10 20
                Console.WriteLine($"{target.MyField1} {target.MyField2}"); // 10 30
            }
        }
    }
}

this 키워드

  • this는 객체가 자신을 지정할 때 사용하는 키워드
  • 객체 외부에서는 객체의 필드(변수)나 메소드를 접근할 때, 객체의 이름(변수, 식별자)을 이용
  • 객체 내부에서는 자신의 필드나 메소드를 접근할 때, this 키워드 사용
  • 예시 코드
class Employee
{
    private string Name; // Employee 클래스의 필드도 Name이고
    private string Position;

    public void SetName(string Name) // SetName 메소드의 매개변수도 Name이므로
    {
        this.Name = Name; // 자신의 필드에 this 붙여서 구별
    }
}

접근 한정자로 공개 scope 설정하기

  • 객체지향 프로그래밍은 클래스 사용자에게 필요한 최소한의 기능만 노출하고, 내부는 감출 것을 요구함
  • 필드는 상수를 제외하고는 무조건 감춤
  • 접근 한정자(Access Modifier)는 감추고 싶은것은 감추고 보여주고 싶은 것은 보여주도록 코드 수식
  • public: 클래스 내부 외부 접근 가능
  • private: 클래스 내부에서만 접근 가능
  • protected: 클래스 내부, 자식 클래스에서만 접근 가능
profile
Backend Web Developer

0개의 댓글