10일차 클래스 커맨트센터, 유닛(공격,피격)
Console Programming/C# Console 2019. 10. 4. 13:53커맨드센터
----------------
이름:
----------------
유닛을만든다
유닛
-----------------
이름:SCV
체력:100
공격력:5
-----------------
공격한다
공격받는다.
|
|
Unit.cs
|
|
|
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 Syntax26
{
class Unit
{
//데이터
//이름
//체력
//공격력
public string name;
int maxHp = 100;
int hp;
int damage = 5;
public Unit()
{
hp = maxHp;
}
//메서드
//이동한다
//공격한다
public void Attack(Unit target)
{
}
public void Hit(int damage, string attackerName)
{
this.hp = this.hp - damage;
}
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
28번 라인의 Target.Hit의 매개변수로 this를 써서 이 클래스 자체를 매개변수로 받아야하는데
this.~~~ 이라고 써서 이름과 데미지를 따로 받아야하게 만들어졌다.
|
|
CommandCenter.cs
|
|
|
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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Syntax26
{
class CommandCenter
{
//데이터
//체력
int hp;
public CommandCenter()
{
}
//메서드
//유닛을 만든다.
public Unit MakeUnit(string name)
{
Unit unit = new Unit();
unit.name = name;
return unit;
}
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
|
|
App.cs
|
|
|
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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Syntax26
{
class App
{
public App()
{
CommandCenter commandCenter1 = new CommandCenter();
Console.WriteLine(commandCenter1 + "이(가) 생성되었습니다. ");
Unit unit1 = commandCenter1.MakeUnit("SCV1");
Unit unit2 = commandCenter1.MakeUnit("SCV2");
}
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|

'Console Programming > C# Console' 카테고리의 다른 글
| 11일차 클래스 피자상점 (0) | 2019.10.04 |
|---|---|
| 10일차 클래스(2) 버거상점, 세트메뉴 (0) | 2019.10.04 |
| 9일차 메서드의 정의(16) 인벤토리, 아이템줍기 (0) | 2019.10.03 |
| 9일차 메서드의 정의(15) 캐릭터 선택, 몬스터 공격 (0) | 2019.10.03 |
| 9일차 메서드의 정의(14) 피격시 영웅의 체력을 감소 시키는 메서드 (0) | 2019.10.03 |

