Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Archives
Today
Total
07-08 04:38
관리 메뉴

nomad-programmer

[Programming/C#] 중첩 클래스 본문

Programming/C#

[Programming/C#] 중첩 클래스

scii 2020. 9. 9. 15:34

중첩 클래스(Nested Class)는 클래스 안에 선언되어 있는 클래스를 말한다. 

class OuterClass
{
    class NestedClass
    {
    }
}

중첩 클래스를 선언하는 문법은 매우 간단하다. 클래스 안에 클래스를 선언하는 것이 전부다. 객체를 생성하거나 객체의 메소드를 호출하는 방법도 보통의 클래스와 다르지 않다.
한 가지 중첩 클래스가 다른 클래스와 다른 점이 있다면, 자신이 소속되어 있는 클래스의 멤버에 자유롭게 접근할 수 있다는 사실이다. 

private 멤버에도 접근할 수 있다.

 

중첩 클래스를 사용하는 이유

  • 클래스 외부에 공개하고 싶지 않은 형식을 만들고자 할 때
  • 현재 클래스의 일부분처럼 표현할 수 있는 클래스를 만들고자 할 때

다른 클래스의 private 멤버에도 접근할 수 있는 중첩 클래스는 은닉성을 무너뜨리기는 하지만, 보다 유연한 표현력을 프로그래머에게 가져다 준다는 장점이 있다.

using System;
using System.Collections.Generic;

namespace CSharpExample
{
    class Configuration
    {
        private List<ItemValue> listConfig = new List<ItemValue>();

        public void SetConfig(string item, string value)
        {
            ItemValue iv = new ItemValue();
            iv.SetValue(this, item, value);
        }

        public string GetConfig(string item)
        {
            foreach (ItemValue iv in listConfig)
            {
                if (iv.GetItem() == item)
                    return iv.GetValue();
            }
            return "";
        }

        // Configuration 클래스 안에 선언된 중첩 클래스. 
        // private으로 선언했기 때문에 Configuration 클래스 밖에서는 보이지 않는다.
        private class ItemValue
        {
            private string item;
            private string value;

            public void SetValue(Configuration config, string item, string value)
            {
                this.item = item;
                this.value = value;
                bool found = false;

                // 중첩 클래스는 상위 클래스의 멤버에 자유롭게 접근할 수 있다.
                for (int i = 0; i < config.listConfig.Count; i++)
                {
                    if (config.listConfig[i].item == item)
                    {
                        config.listConfig[i] = this;
                        found = true;
                        break;
                    }
                }
                if (found == false)
                {
                    config.listConfig.Add(this);
                }
            }

            public string GetItem()
            {
                return item;
            }

            public string GetValue()
            {
                return value;
            }
        }
    }

    class MainApp
    {
        static int Main(string[] args)
        {
            Configuration config = new Configuration();
            config.SetConfig("version", "v 5.0");
            config.SetConfig("size", "655,324 KB");

            Console.WriteLine(config.GetConfig("version"));
            Console.WriteLine(config.GetConfig("size"));

            config.SetConfig("version", "v 5.0.1");
            Console.WriteLine(config.GetConfig("version"));

            return 0;
        }
    }
}


/* 결과

v 5.0
655,324 KB
v 5.0.1

*/
Comments