본문 바로가기
Programming Language/C#

[C#] 중첩 클래스

by dbxxrud 2019. 5. 18.

중첩 클래스 - 클래스 안에 선언되어 있는 클래스를 말한다. 선언하는 문법은 매우 간단하다. 클래스 안에 클래스를 선언하는 것이 전부이기 때문이다. 중첩 클래스가 다른 클래스와 다른 점이 있다면, 자신이 소속되어 있는 클래스의 멤버에 자유롭게 접근할 수 있다는 것이다. private 멤버에도 접근 가능하다.

 

 

 

선언

 

위와 같이 중첩 클래스를 선언하는 문법은 매우 간단하다. 클래스 안에 클래스를 선언하는 것이다. 객체를 생성하거나 객체의 메서드를 호출하는 방법도 보통의 클래스와 다르지 않다.

 

 

 

중첩 클래스에서 자신의 소속 클래스 멤버의 접근하는 예제이다.

 

 

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

 

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

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

 

 

예제

 

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using System.Collections.Generic;
 
namespace NestedClass
{
    class Configuration
    {
        List<ItemValue> listConfig = new List<ItemValue>(); // 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 "";
        }
 
        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 void 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"));
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

Configuration에서 ItemValue 리스트를 생성을 했고 내부에 ItemValue 클래스를 중첩 클래스로 가지고 있다. ItemValue 클래스 안에는 SetValue, GetItem, GetValue 세 가지의 메서드가 선언이 되어 있다. 메인 클래스 안에서 Configuration의 인스턴스를 생성해 SetConfig 메서드를 출력함으로써 값을 설정해주고 있으며 GetConfig 메서드로 출력을 한다.

SetConfig와 GetConfig는 중첩 클래스인 ItemValue의 인스턴스를 만들어서 내부의 있는 메서드를 호출하고 있으며, 중첩 클래스인 ItemValue는 외부 클래스인 Configuration클래스의 리스트 컬렉션에 접근하고 있는 구조이다.

 

'Programming Language > C#' 카테고리의 다른 글

[C#] 인터페이스 (Interface)  (0) 2019.05.18
[C#] 구조체  (0) 2019.05.18
[C#] 오버라이딩  (0) 2019.05.18
[C#] 상속  (0) 2019.05.18
[C#] 접근 한정자  (0) 2019.05.18