(C#) 20. 상속성 (Inheritance)
inheritance
(inheritance) : OOP
- 최초 작성일: 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System;
namespace Inheritance
{
class Program
{
// OOP 상속성 (은닉성 / 상속성 / 다형성)
class Player // 부모 클래스 혹은 기반 클래스
{
static public int counter = 1; // 오로지 1개만 존재!
public int id;
public int hp;
public int attack;
public void Move()
{
Console.WriteLine("Player Move");
}
public void Attack()
{
Console.WriteLine("Player Attack");
}
public Player()
{
Console.WriteLine("Player 생성자 호출!");
}
public Player(int hp)
{
this.hp = hp;
Console.WriteLine("Player hp 생성자 호출!");
}
}
class Mage : Player
{
}
class Archer : Player
{
}
class Knight : Player // 자식, 파생
{
public Knight() : base(100)
{
Console.WriteLine("Knight 생성자 호출!");
}
// static 함수 -> 클래스에 종속적 (유일성)
static public Knight CreateKnight()
{
Knight knight = new Knight();
knight.hp = 100;
knight.attack = 1;
return knight;
}
public Knight Clone()
{
Knight knight = new Knight();
knight.hp = hp;
knight.attack = attack;
return knight;
}
}
static void Main(string[] args)
{
Knight knight = new Knight();
knight.Move();
}
}
}
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.