namespace inheritEx
{
class Parent
// ์์์ ํด์ฃผ๋ ํด๋์ค๋ฅผ ๋ถ๋ชจ, ์ํผ, ์์ ํด๋์ค๋ผ๊ณ ์ผ์ปซ๋๋ค.
// ๊ธฐ๋ฐ(Base) ํด๋์ค๋ผ๊ณ ๋ ๋ถ๋ฅธ๋ค.
{
public Parent()
{
Console.WriteLine("Parent ์์ฑ์ ํธ์ถ");
}
~Parent()
{
Console.WriteLine("Parent ์ข
๋ฃ์ ํธ์ถ");
}
public void showInfo()
{
Console.WriteLine("Parent Class");
}
}
class Child : Parent
// ์์์ ๋ฐ๋ ํด๋์ค๋ฅผ ์์(ํ์)ํด๋์ค๋ผ๊ณ ์ผ์ปซ๋๋ค.
// ์์์ ๋ฐ์ ๋์๋ : ํค์๋๋ฅผ ์ด์ฉํ์ฌ ์์๋ฐ๋๋ค.
// ์์๋ฐ์ ์ฝ๋๋ฅผ ์ฌํ์ฉํด์ ์์ ์๊ฒ ๋ง๋ ์ฝ๋๋ก ์์ ํ์ฌ ์ฌ์ฉํ ์ ์๋ค.
{
public Child()
{
Console.WriteLine("Child ์์ฑ์ ํธ์ถ");
}
~Child()
{
Console.WriteLine("Child ์ข
๋ฃ์ ํธ์ถ");
}
public void showInfo() { Console.WriteLine("Child Class"); }
}
class Program
{
static void Main(string[] args)
{
Child a = new Child();
a.showInfo();
}
}
}
Output
Parent ์์ฑ์ ํธ์ถ
Child ์์ฑ์ ํธ์ถ
Child Class
์์
: ๋ถ๋ชจ๋ก๋ถํฐ ์์์ ๋ฐ๋ฏ์ด ํ๋ก๊ทธ๋๋ฐ์์๋ ๋น์ทํ๊ฒ ๋ถ๋ชจํด๋์ค์์ ์์ํด๋์ค์๊ฒ ๊ฐ์ง๊ณ ์๋ ํ๋๋ ๋ฉ์๋๋ฅผ ์ ๋ฌํจ์ผ๋ก์จ์ฌํ์ฉ
ํ๋๊ฒ.- ์์์ ํด์ฃผ๋ ํด๋์ค๋ฅผ
๋ถ๋ชจ ํด๋์ค
(ํน์์ํผ(super)ํด๋์ค
,์์ํด๋์ค
,๊ธฐ๋ฐ(Base)ํด๋์ค
)๋ผ๊ณ ์ผ์ปซ๋๋ค. - ์์์ ๋ฐ๋ ํด๋์ค๋ฅผ
์์ํด๋์ค
(ํน์ํ์ํด๋์ค
,ํ์ํด๋์ค
) ๋ผ๊ณ ์ผ์ปซ๋๋ค. - ์์์ ๋ฐ์ ๋์๋
:
ํค์๋๋ฅผ ์ด์ฉํ์ฌ ๋ถ๋ชจํด๋์ค๋ฅผ ์์ ๋ฐ๋๋ค. ์์
์ ๊ฐ์ฅ ํฐ ๋ชฉ์ ์๋ถ๋ชจํด๋์ค
์ ์ฝ๋๋ฅผ ์ฌํ์ฉํ์ฌ์์ํด๋์ค
์๊ฒ ๋ง๋ ์ฝ๋๋ก ์์ ํด ์ฌ์ฉํจ์ ์๋ค.(์ฌํ์ฉ
)
'๐ป Programming Language > C#' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
09. ์์๊ด๊ณ์ ํด๋์ค ํ๋ณํ (0) | 2022.04.06 |
---|---|
08. baseํค์๋์ sealedํ์ ์ (0) | 2022.04.06 |
06. ์ ๊ทผ์ ํ์(Access Modifier) (0) | 2022.04.06 |
05. this, this() ์์ฑ์ (0) | 2022.04.06 |
04. ๊น์ ๋ณต์ฌ์ ์ดํด (Deep Copy) (0) | 2022.04.06 |