10.17 상속 클래스를 이용한 뽑기박스
C#/실습 2019. 10. 17. 23:10예제:
Equipment 클래스에 상속된 두 아이템 타입(Weapon, Armor)을 만들고 List<Equipment>타입으로 저장한다.
뽑기박스 클래스에 상속된 두 박스(WeaponBox, ArmorBox) 를 만들어 각각의 박스가 해당되는 아이템만을 출력하게 만든다.
코드:
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
|
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp1
{
class App
{
public App()
{
List<Equipment> equipments = new List<Equipment>();
List<Equipment> weapons = new List<Equipment>();
Weapon weapon1 = new Weapon("활");
Weapon weapon2 = new Weapon("검");
Weapon weapon3 = new Weapon("총");
Armor armor1 = new Armor("가죽옷");
Armor armor2 = new Armor("사슬옷");
Armor armor3 = new Armor("다이아옷");
{
if(equipments[i].GetType() == typeof(Weapon))
{
}//GetType을 이용해 타입을 반환하고 반환한 타입을 typeof를 이용해 그 타입 자체와 비교
}
Console.WriteLine("모든 아이템:");
foreach (var output in equipments)
{
Console.WriteLine(output.GetName());
}
WeaponBox weaponBox = new WeaponBox();
foreach (var item in weapons)
{
}
weaponBox.OpenBox();
Console.Read();
}
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp1
{
class Equipment
{
string name;
public Equipment(string name)
{
this.name = name;
}
public string GetName()
{
return this.name;
}
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp1
{
class Weapon: Equipment
{
public Weapon(string name) : base(name)
{
}
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp1
{
class Armor : Equipment
{
public Armor(string name) : base(name)
{
}
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp1
{
class LootBox
{
public List<Equipment> equipments;
public LootBox()
{
this.equipments = new List<Equipment>();
}
public virtual void OpenBox()
{
foreach(var item in equipments)
{
Console.WriteLine($"상자를 열어서 {item.GetName()}을 획득했습니다.");
}
}
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp1
{
class WeaponBox : LootBox
{
public WeaponBox()
{
}
public override void OpenBox()
{
Console.WriteLine("무기상자를 열었습니다.");
base.OpenBox();
}
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp1
{
class ArmorBox :LootBox
{
public ArmorBox()
{
}
public override void OpenBox()
{
base.OpenBox();
}
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
출력:

'C# > 실습' 카테고리의 다른 글
10.24 벡터 (0) | 2019.10.24 |
---|---|
2048 프로그램 (저장기능x) (0) | 2019.10.23 |
10.15 Dictionary 활용해 업적 보상 UIPanel 만들기 (0) | 2019.10.15 |
10.14 클오클 클랜원 UIPanel 만들기 (0) | 2019.10.14 |
10.10 class를 활용해 인벤토리와 기능 만들기 (0) | 2019.10.10 |