์ฐ์ฐ์ | ์ค๋ช |
is | ๊ฐ์ฒด๊ฐ ํด๋น ํ์์ ํด๋นํ๋์ง ๊ฒ์ฌํ์ฌ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ bool ๊ฐ์ผ๋ก ๋ฐํํ๋ค (๊ฐ์ฒด(ํ์ ๊ฐ๋ฆฌํค๋ ๋ณ์)๊ฐ ์ฐพ๊ณ ์๋ ์์ฑ์ ํ์์ ๊ฐ๋ฆฌํค๋๊ฒ ๋ง๋์ง๋ฅผ ํ์ธ!!! ๋ค๋ฅธ ์์ฑ์ ํ์์ ๊ฐ๋ฆฌํฌ ์๋ ์์ผ๋๊น) |
as | ์บ์คํธ. ๋ง๊ทธ๋๋ก ํ์ ๋ณํ! ํ์ ๋ณํ ์ฐ์ฐ์์ ๊ฐ์ ์ญํ ์ ํ๋ค. (์๋ฌด๋ฆฌ ์์ฑ์ ํ์์ ๊ฐ๋ฅดํฌ์ง ์ธ์ ์๋ฒฝํ๊ฒ ํ๋ณํํ๊ธฐ ์ํด์๋ ๋ณ์ ์์ฒด๋ ๊ทธ ์ ์ธ์ ๊ฐ์ ํ์ผ๋ก ์ง์ ํด์ฃผ์ด์ผ ํ๋ค.) ๋ค๋ง ํ์ ๋ณํ ์ฐ์ฐ์๊ฐ ๋ณํ์ ์คํจํ๋ ๊ฒฝ์ฐ ์์ธ๋ฅผ ๋์ง๋ ๋ฐ๋ฉด์ as ์ฐ์ฐ์๋ ๊ฐ์ฒด ์ฐธ์กฐ๋ฅผ null๋ก ๋ง๋ ๋ค๋ ๊ฒ์ด ๋ค๋ฅด๋ค. |
as ์ฌ์ฉ ๋ฐฉ๋ฒ
Mammal mammal3 = new Cat();
if (mammal3 is Cat)
{
Cat cat2 = mammal3 as Cat;
cat2.Meow();
}
Mammal mammal3 = new Cat();
if (mammal3 is Cat)
{
Cat cat2 = (Cat)mammal3;
cat2.Meow();
}
๊ธฐ๋ฐ ํด๋์ค ๋ณ์์๋ค๋ฉด
๊ฒฐ๊ตญ ์์ ํด๋์ค ์์ฑ์๋ฅผ ํ ๋นํ๊ธฐ ์ํด new ์์ํด๋์ค์๋ณ์() ๋ถ๋ฌ์ ์ฐธ์กฐ ์ค์ ๋ฐ๊ฟ์ฃผ๊ณ ,
๋ค์ ์์ํด๋์ค ๋ณ์์๋ค as ํค์๋ ์ฐ๊ฑฐ๋ (๊ฐ์ ํ๋ณํ)ํค์๋๋ฅผ ์จ์ ๋ณต์ฌํด์ฃผ์ด์ผ ํ๋ค.~~~
๋๋ค ์ถ๋ ฅ ๊ฒฐ๊ณผ๊ฐ ๋๊ฐ๊ณ
Meow()๋ฅผ ์ ์์ ์ผ๋ก ํธ์ถํ๋ค.
์ผ๋ฐ์ ์ผ๋ก (Dog)๋ (Mammal) ๊ผด๋ก ์ํํ๋ ํ์ ๋ณํ ๋์ as ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ๋ ์ชฝ์ ๊ถ์ฅํ๋ค!
(๋น ์๊ทธ๋, [Effective C3]์ฐธ์กฐ)
ํ์ ๋ณํ์ ์คํจํ๋๋ผ๋ ์์ธ๊ฐ ์ผ์ด๋ ๊ฐ์๊ธฐ ์ฝ๋์ ์คํ์ด ์ ํํ๋ ์ผ์ด ์์ผ๋ฏ๋ก
์ฝ๋๋ฅผ ๊ด๋ฆฌํ๊ธฐ๊ฐ ๋ ์์ํ๊ธฐ ๋๋ฌธ์ด๋ค.
๋จ, as ์ฐ์ฐ์๋ ์ฐธ์กฐ ํ์์ ๋ํด์๋ง ์ฌ์ฉ์ด ๊ฐ๋ฅํ๋ฏ๋ก
๊ฐ ํ์์ ๊ฐ์ฒด๋ ๊ธฐ์กด์ ํ์ ๋ณํ ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace UsingOut
{
class Mammal
{
public void Nurse()
{
WriteLine("์์์ ์์กํ๋ ์ค...");
}
}
class Dog: Mammal
{
public void Bark()
{
WriteLine("Bark!");
}
}
class MainApp
{
static void Main(string[] args)
{
Dog dog = new Dog();
Mammal mammal = dog as Mammal;
if (mammal != null)
{
mammal.Nurse();
}
else
{
WriteLine("์ด์ํ ์ฝ๋");
}
}
}
}
<์ถ๋ ฅ ๊ฒฐ๊ณผ>
์์์ ์์กํ๋ ์ค...
<์์ >
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace UsingOut
{
class Mammal
{
public void Nurse() { }
}
class Dog: Mammal
{
public void Bark()
{
WriteLine("Bark!");
}
}
class Rabbit : Mammal
{
public void Jump() { }
}
class Whale : Mammal
{
public void Swim() { }
}
class Cat : Mammal
{
public void Meow()
{
WriteLine("Meow!");
}
}
class MainApp
{
static void Main(string[] args)
{
Mammal mammal1 = new Dog();
Dog dog;
if(mammal1 is Dog)
{
dog = (Dog)mammal1; //mammal1 ๊ฐ์ฒด๊ฐ Dog ํ์์์ ํ์ธํ์ผ๋ฏ๋ก ์์ ํ๊ฒ ํ๋ณํ
dog.Bark();
}
Mammal mammal2 = new Cat();
Cat cat = mammal2 as Cat;
if (cat != null) //mammal2๊ฐ Cat ํ์ ๋ณํ์ ์คํจํ๋ค๋ฉด cat์ null์ด ๋๋ค.
{ //ํ์ง๋ง ์ด ์ฝ๋์์ mammal2๋ Catํ์์ ํด๋นํ๋ฏ๋ก ์์ ํ๊ฒ ์บ์คํธ๋์์ผ๋ฏ๋ก
cat.Meow(); //(1)๋ํ๋จ
}
//mammal1 = new Cat(); //ํด๋น ์ฃผ์์ ํ๋ฉด (2)์ด ๋ํ๋จ
Cat cat2 = mammal1 as Cat;
if (cat2 != null)
{
cat2.Meow(); //(2)๋ํ๋์ง ์์
}
else
WriteLine("cat2 is not a Cat!");
}
}
}
<์ถ๋ ฅ>
Bark!
Meow!
cat2 is not a Cat!
<๋ณํ ์์ >
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace UsingOut
{
class Mammal
{
public void Nurse() { }
}
class Dog: Mammal
{
public void Bark()
{
WriteLine("Bark!");
}
}
class Rabbit : Mammal
{
public void Jump() { }
}
class Whale : Mammal
{
public void Swim() { }
}
class Cat : Mammal
{
public void Meow()
{
WriteLine("Meow!");
}
}
class MainApp
{
static void Main(string[] args)
{
Mammal mammal1 = new Dog();
Dog dog;
if(mammal1 is Dog)
{
dog = (Dog)mammal1;
dog.Bark();
}
Mammal mammal2 = new Cat();
Cat cat = mammal2 as Cat;
if (cat != null)
{
cat.Meow(); //(1)๋ํ๋จ
}
mammal1 = new Cat(); //ํด๋น ์ฃผ์์ ํ๋ฉด (2)์ด ๋ํ๋จ
Cat cat2 = mammal1 as Cat;
if (cat2 != null)
{
cat2.Meow(); //(2)๋ํ๋จ
}
else
WriteLine("cat2 is not a Cat!"); //(3)๋ํ๋์ง ์์
}
}
}
<์ถ๋ ฅ ๊ฒฐ๊ณผ>
Bark!
Meow!
Meow!
์์ฝ
<1>
Mammal mammal1 = new Dog();
Dog dog;
if(mammal1 is Dog)
{
dog = (Dog)mammal1; //mammal1 ๊ฐ์ฒด๊ฐ Dog ํ์์์ ํ์ธํ์ผ๋ฏ๋ก
//์์ ํ๊ฒ ํ์ ๋ณํ์ ํด์ค๋ค.
dog.Bark();
}
Bark!
<2>
Mammal mammal1 = new Dog();
if(mammal1 is Dog)
{
Dog dog = (Dog)mammal1;
dog.Bark();
}
Bark!
<3>
Mammal mammal1 = new Dog();
if(mammal1 is Dog)
{
Dog dog = mammal1 as Dog;
dog.Bark();
}
Bark!
<๊ธฐํ ์ฐ์ต>
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace UsingOut
{
class Mammal
{
public void Nurse() { }
}
class Dog: Mammal
{
public void Bark()
{
WriteLine("Bark!");
}
}
class Rabbit : Mammal
{
public void Jump()
{
WriteLine("๊นก์ถฉ๊นก์ถฉ!");
}
}
class MainApp
{
static void Main(string[] args)
{
Mammal mammal1 = new Dog();
Dog dog = mammal1 as Dog;
dog.Bark();
Mammal mammal2 = new Rabbit();
Rabbit rabbit = mammal2 as Rabbit;
if (rabbit != null)
{
rabbit.Jump();
}
else
{
WriteLine("์ด์ํ ์ฝ๋");
}
}
}
}
<์ถ๋ ฅ ๊ฒฐ๊ณผ>
Bark!
๊นก์ถฉ๊นก์ถฉ!
<๊ธฐํ ์ฐ์ต2>
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace UsingOut
{
class Mammal
{
public void Nurse() { }
}
class Dog: Mammal
{
public void Bark()
{
WriteLine("Bark!");
}
}
class Rabbit : Mammal
{
public void Jump()
{
WriteLine("๊นก์ถฉ๊นก์ถฉ!");
}
}
class MainApp
{
static void Main(string[] args)
{
Mammal mammal1 = new Dog();
Dog dog = mammal1 as Dog;
dog.Bark();
Mammal mammal2 = new Dog();
Rabbit rabbit = mammal2 as Rabbit;
if (rabbit != null)
{
rabbit.Jump();
}
else
{
WriteLine("์ด์ํ ์ฝ๋");
}
}
}
}
<์ถ๋ ฅ ๊ฒฐ๊ณผ>
Bark!
์ด์ํ ์ฝ๋
'C# > ์ด๊ฒ์ด C#์ด๋ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
26. ์ค๋ฒ๋ผ์ด๋ฉ ๋ด์ธํ๊ธฐ (0) | 2022.02.11 |
---|---|
25. ์ค๋ฒ๋ผ์ด๋ฉ๊ณผ ๋คํ์ฑ virtual, override (0) | 2022.02.11 |
[์ค๊ฐ์ ๊ฒ] ์ ๊ทผํ์ ์ internal์์ ๊ฐ์ ์ด์ ๋ธ๋ฆฌ๋ (0) | 2022.02.11 |
23. ์์, ์ฝ๋ ์ฌํ์ฉ, ๊ธฐ๋ฐ ํด๋์ค, ํ์ ํด๋์ค, base, sealed (0) | 2022.02.11 |
22. ์๋์ฑ, ์ ๊ทผ ํ์ ์ (0) | 2022.02.10 |