1) 클래스 Encapsulation
클래스에서 Encapsulation class안에 있는 멤버 변수에 대해서 은닉을 한다는 뜻 입니다.
내가 설명하고 너무 어렵게 말하는것 같습니다. 정리를 조금 하면 클래스 내의 각 변수 또는 함수에 대해서 숨기거나 외부에 노출할 함수를 각각 설정할수 있게 한다는 의미입니다.
- Public : 클래스 외부에서 접근이 가능하다.
- Private : 클래스 내부에서만 접근이 가능하다.
- Protected : 상속을 통해서만 접근이 가능하다.
- Internal : 동일한 Assembly안에서만 접근이 가능하다.
- Protected Internal : 동일한 Assembly와 상속한 클래스에서 사용이 가능하다.
<Sample Code>
-Public Access Specifier
using System; namespace RectangleApplication { class Rectangle { //public 으로 선언했다. public double length; public double width; public double GetArea() { return length * width; } public void Display() { Console.WriteLine("Length: {0}", length); Console.WriteLine("Width: {0}", width); Console.WriteLine("Area: {0}", GetArea()); } } class ExecuteRectangle { static void Main(string[] args) { Rectangle r = new Rectangle(); r.length = 4.5; //public 으로 선언한 것에 바로 접근이 가능하다. r.width = 3.5; //public 으로 선언한 것에 바로 접근이 가능하다. r.Display(); Console.ReadLine(); } } }
-Private Access Specifier
using System; namespace RectangleApplication { class Rectangle { //private로 선언했다. private double length; private double width; public void Acceptdetails() { Console.WriteLine("Enter Length: "); length = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("Enter Width: "); width = Convert.ToDouble(Console.ReadLine()); } public double GetArea() { return length * width; } public void Display() { Console.WriteLine("Length: {0}", length); Console.WriteLine("Width: {0}", width); Console.WriteLine("Area: {0}", GetArea()); } } class ExecuteRectangle { static void Main(string[] args) { Rectangle r = new Rectangle(); r.Acceptdetails(); // Acceptdetails() 함수를 통해서 length, width에 접근한다. r.Display(); // Display() 함수를 통해서 length, width에 접근한다. Console.ReadLine(); } } }
Protected에 대해서는 따로 Inheritance(상속)에서 설명하겠다.
'C#(CSharp) > 기초강좌(Basic)' 카테고리의 다른 글
[12] C# Nullables (널 가능) (0) | 2015.02.23 |
---|---|
[11] C# 메소드 Method (Call by Value, Call by Reference, Output Parameter) (0) | 2015.02.23 |
[9] C# 반복문 컨트롤 Statement (break, continue) (0) | 2015.02.23 |
[8] C# 반복문 (C# Loop) (0) | 2015.02.23 |
[7] C# 조건문 (C# if else statement) (0) | 2015.02.23 |