(C#) 22. 클래스 형식 변환
class type conversion
#
- 최초 작성일: 2021년 3월 21일(월)
##
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
namespace ClassTypeConv
{
// 클래스 형식 변환
// OOP (은닉성 / 상속성 / 다형성)
class Player
{
protected int hp;
protected int attack;
}
class Knight : Player
{
}
class Mage : Player
{
public int mp;
}
class Program
{
static void EnterGame(Player player)
{
//bool isMage = (player is Mage);
//if (isMage)
//{
// Mage mage = (Mage)player;
// mage.mp = 10;
//}
Mage mage = (player as Mage);
if (mage != null)
{
mage.mp = 10;
}
}
static void Main(string[] args)
{
Knight knight = new Knight();
Mage mage = new Mage();
// Mage type -> Player type 가능
// Player type -> Mage type ? Case by case
EnterGame(knight);
}
}
}
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.