namespace AsisEx
{
class Human
{
public void showInfo()
{
Console.WriteLine("Human");
}
}
class Programmer : Human
{
public void programming()
{
Console.WriteLine("๊ฐ๋ฐ์");
}
}
class Designer : Human
{
public void design()
{
Console.WriteLine("๋์์ด๋");
}
}
class Program
{
static void Main(string[] args)
{
Human human = new Programmer();
//ํ์ํด๋์ค ์ธ์คํด์ค๋ฅผ ๊ธฐ๋ฐํด๋์ค ์ธ์คํด์ค๋ก ์ ์ธ
Programmer programmer;
if (human is Programmer)
{
programmer = (Programmer)human;
programmer.programming();
}
Console.WriteLine(human is Human);
Console.WriteLine(human is Programmer);
Human human2 = new Designer();
// Designer ์ธ์คํด์ค๋ฅผ Human ์ธ์คํด์ค๋ก ์ ์ธ
Designer designer = human2 as Designer;
// ํด๋น Human ์ธ์คํด์ค๋ฅผ Designer ํ์
์ผ๋ก ํ๋ณํ
if (designer != null)
{
designer.design();
}
Designer designer2 = human as Designer;
// human์ Programmer ํ์์ผ๋ก ์ ์ธํ๊ธฐ์ Designer๋ก ํ๋ณํ์ด ์๋จ.
// ๋ฐ๋ผ์ designer2๋ null ๊ฐ์ ๊ฐ์ง๊ฒ ๋จ.
if (designer2 != null)
{
designer2.design();
}
else
{
Console.WriteLine("designer2๋ Designer ํ์ด ์๋๋ค.");
}
}
}
}
Output
๊ฐ๋ฐ์
True
True
๋์์ด๋
designer2๋ Designer ํ์ด ์๋๋ค.
as
: ํ๋ณํ(Casting)๊ณผ ๊ฐ์ ์ญํ ์ ํ๋ ์ฐ์ฐ์
- ํ๋ณํ์ ์คํจํ์ ๊ฒฝ์ฐ null ๊ฐ์ ๋ฐํํ๋ค.
is
: ํด๋น ๊ฐ์ฒด์ Type(ํ์)์ด ์ผ์นํ๋์ง ์ฌ๋ถ๋ฅผ ๊ฒ์ฌ
- ์ผ์น์ฌ๋ถ๋ bool๊ฐ์ผ๋ก ๋ฐํํ๋ค.