수업 007 (04.13 2020) Barracks (enum, id, unit type, return)
2020. 4. 13. 15:18
728x90
1. Class 'App'
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.Linq;
using System.Text;
namespace Study._8_2
{
class App
{
public App()
{
Barracks barracks = new Barracks();
var marine1 = barracks.CreateUnit("1");
var marine2 = barracks.CreateUnit("2");
}
}
}
|
2. Class 'Barranks'
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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Study._8_2
{
class Barracks
{
public int idCount = 0;
// 배럭스 입력받은 번호에 따라서 해당 타입의 유닛을 만든다
public Barracks()
{
Console.WriteLine("배럭스가 생성되었습니다.");
}
// 유닛을 생성함
public Unit CreateUnit(string strSekectedNum) // *** void 에서 return에 따라 데이터 타입이 변함
{
// 문자열 "1", "2"
// 전달 받은 문자열에 따라 유닛타입이 결정
// = eUnitType unitType = (eUnitType) int.parse.(strSelectNum)
Console.WriteLine("유닛을 생성합니다.");
// id를 하나씩 증가시키기
#region ***'return'
/*
public 반환타입 CreatUnit (UnitType unitType)
{
Unit unit = new Unit(); // 변수안에는 항상 값이 있음
return 반환값;
}
*/
#endregion
Unit unit = new Unit(idCount, unitType); // new Unit의 인스터스=값
idCount++;
return unit;
}
}
}
|
<Return>
public 반환타입 CreatUnit (UnitType unitType)
{
Unit unit = new Unit(); // 변수안에는 항상 값이 있음
return 반환값;
}
3. Class 'Unit'
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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Study._8_2
{
public enum eUnitType
{
None,
Marine, // or Marin = 1 (None 없이)
Madic
}
class Unit
{
public int id;
public eUnitType unitType;
// 생성자
public Unit(int id, eUnitType unitType)
{
this.id = id;
this.unitType = unitType;
Console.WriteLine("{0}유닛({1})이 생성되었습니다.", this.unitType, this.id);
}
public void Attack(Unit target) // 나=0 타겟=1
{
target.Hit(this);
}
public void Hit(Unit target) // 나=1 타겟=0
{
Console.WriteLine("{0}{1}이 {2}{3}에게 피해를 받았습니다.", this.unitType, this.id, target.unitType, target.id);
}
}
}
|
728x90
'C# > Study' 카테고리의 다른 글
수업 007 (04.13 2020) Hatchery, Lava, Drone (0) | 2020.04.13 |
---|---|
수업 007 (04.13 2020) TankMode/SeizeMode (0) | 2020.04.13 |
수업 007 (04.13 2020) Hong vs Lim Again (0) | 2020.04.13 |
수업 004 (04.08 2020) (0) | 2020.04.08 |
수업 003 (04.07 2020) (0) | 2020.04.07 |