namespace AccessModifierEx
{
class Demo
{
// public ์ ๊ทผ ์ ํ์
public static void public_method() { }
// private ์ ๊ทผ ์ ํ์
private static void private_method() { }
// protected ์ ๊ทผ ์ ํ์
protected static void protected_method() { }
// internal ์ ๊ทผ ์ ํ์
internal static void internal_method() { }
// protected internal ์ ๊ทผ ์ ํ์
protected internal static void protected_internal_method() { }
// private protected ์ ๊ทผ ์ ํ์
private protected static void private_protected_method() { }
public Demo()
{
// ํด๋์ค ์์ฒด ๋ด๋ถ์์ ์ ๊ทผ
Demo.public_method();
Demo.private_method();
Demo.protected_method();
Demo.internal_method();
Demo.protected_internal_method();
Demo.private_protected_method();
// ์์ฒด ํด๋์ค ๋ด๋ถ์์๋ ์ ๊ทผํ๋๋ฐ์ ๋ฌธ์ ๊ฐ ์์.
}
}
class Demo2 : Demo
{
public Demo2()
{
// ์์๋ฐ์ ์์(ํ์) ํด๋์ค์์ ์ ๊ทผ
Demo.public_method();
Demo.protected_method();
Demo.internal_method();
Demo.protected_internal_method();
Demo.private_protected_method();
// ์ด ํด๋์ค ํ์ผ์ ๊ฐ์ ์ปดํ์ผ๋ฌ์์ ์ด์
๋ธ๋ฆฌ๋ก ๋ง๋ค์ด์ง๊ธฐ์ ์ด 5๊ฐ ๋ฉ์๋ ์ ๊ทผ๊ฐ๋ฅ
}
}
class Program
{
static void Main(string[] args)
{
// ์์๋ฐ์ง ์์ ์ธ๋ถํด๋์ค์์ ์ ๊ทผ
Demo.public_method();
Demo.internal_method();
Demo.protected_internal_method();
// Main ๋ฌธ๋ ๊ฒฐ๊ตญ ๊ฐ์ ์ด์
๋ธ๋ฆฌ๋ด์ ์กด์ฌํจ.
// ๋ฐ๋ผ์, public์ ํฌํจํ ์ด 3๊ฐ ๋ฉ์๋ ์ ๊ทผ๊ฐ๋ฅ
}
}
}
์ ๊ทผ์ ํ์
๋ํ์ ์
๋ผ๊ณ ํํํ๊ธฐ๋ ํ๋ค.- ์๋ฌธ ํ๊ธฐ๋ก
Access Modifier
๋ผ๊ณ ํ๋ค. ์ ๊ทผ์ ํ์
๋ ๊ณต๊ฐ์์ค์ ์ ํ์ฌ์ ๊ทผ์ ์ ํ
ํ๋ ํค์๋๋ฅผ ๋งํ๋ค.- ์ข
๋ฅ๋ก๋
public, private, protected, internal, protected internal, private protected
๊ฐ ์๋ค. public
,private
,protected
๋ฅผ ์ฃผ๋ก ์ฌ์ฉ
โ
โ
โ
ํด๋์ค ๋ฉค๋ฒ์ ํ์ ์๊ฐ ์ค์ ๋์ง ์์์ ๊ฒฝ์ฐ์๋ ๋ฌด์กฐ๊ฑด private
์ผ๋ก ์๋ ์ง์ ๋๋ค.
public
:- ํด๋์ค์ ๋ด๋ถ ๋๋ ์ธ๋ถ ๋ชจ๋ ๊ณณ์์ ์ ๊ทผํ ์ ์๋ ์ง์ ์
private
:- ์บก์ํํ ๋ ์ฃผ๋ก ์ฌ์ฉํ๋ฉฐ ํด๋์ค ์ธ๋ถ์์๋ ์ ๊ทผํ ์ ์๋๋ก ํ๋ ์ง์ ์
- ์ฆ, ๋ด๋ถ์์๋ง ์ ๊ทผ์ด ๊ฐ๋ฅํ๋๋ก ํ๋ ์ง์ ์
- ๋ํ ์์๋ฐ์ ์์(ํ์) ํด๋์ค์์๋ ์ ๊ทผํ ์ ์๋ค.
protected
:- ํด๋์ค ์ธ๋ถ์์๋ ์ ๊ทผํ ์ ์๋ ์ง์ ์
- ๋จ, ์์๋ฐ์ ์์(ํ์) ํด๋์ค์์๋ ์ ๊ทผํ ์ ์๋๋ก ํ๋ ์ง์ ์.
internal
:- ๋์ผ ์ด์ ๋ธ๋ฆฌ์ ์๋ ์ฝ๋์์๋ง public์ผ๋ก ์ ๊ทผํ ์ ์๋๋ก ํ๋ ์ง์ ์.
- ๋ค๋ฅธ ์ด์ ๋ธ๋ฆฌ์ ์๋ ์ฝ๋์์๋ private๊ณผ ๊ฐ์ ์ ๊ทผ ์์ค์ ๊ฐ์ง๊ธฐ์ ์ ๊ทผ์ด ์๋จ.
- ๋ํ internal ํค์๋๊ฐ ๊ฐ์ namespace ๋ด์์๋ง ์ ๊ทผ๊ฐ๋ฅํ๋ค๋ ๋ง์ ํ๋ฆฐ๋ง์.
protected internal
:- ๋์ผ ์ด์ ๋ธ๋ฆฌ์ ์๋ ์ฝ๋์์๋ง protected๋ก ์ ๊ทผํ ์ ์๋ ์ง์ ์
private protected
:- ๋์ผ ์ด์ ๋ธ๋ฆฌ์ ์๋ ํด๋์ค์์ ์์๋ฐ์ ํด๋์ค ๋ด๋ถ์์๋ง ์ ๊ทผ์ด ๊ฐ๋ฅ
'๐ป Programming Language > C#' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
08. baseํค์๋์ sealedํ์ ์ (0) | 2022.04.06 |
---|---|
07. ์์ (0) | 2022.04.06 |
05. this, this() ์์ฑ์ (0) | 2022.04.06 |
04. ๊น์ ๋ณต์ฌ์ ์ดํด (Deep Copy) (0) | 2022.04.06 |
03. static ํ๋์ static ๋ฉ์๋ (0) | 2022.04.06 |