전체 코드

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharp
{
    // 플레이어 생성
    public enum PlayerType
    {
        None = 0,
        Knight = 1,
        Archer = 2,
        Mage = 3
    }
    class Player
    {
        protected PlayerType _type = PlayerType.None;
        protected int hp = 0;
        protected int attack = 0;

        // 외부에서 만들어줄 수 없음 -> 아래의 직업 클래스가 간접적으로 만들어야함

        protected Player(PlayerType type)
        {
            // 컨벤션 규정임
           _type = type;
        }

        public void SetInfo(int hp, int attack)
        {
            this.hp = hp;
            this.attack = attack;
        }

        public int GetHP() { return hp; }
        public int GetAttack() { return attack; }
        public PlayerType GetPlayerType() { return _type; }

        public bool IsDead() { return hp <= 0; }

        public void OnDamaged(int damage)
        {
            hp -= damage;
            if (hp < 0)
            {
                hp = 0;
            }
        }
    }
    class Knight : Player
    {
        public Knight() : base(PlayerType.Knight)
        {
            //type = PlayerType.Knight;
            SetInfo(100, 10);
        }
    }

    class Archer : Player
    {
        public Archer() : base(PlayerType.Archer)
        {
            //type = PlayerType.Archer;

            SetInfo(75, 12);
        }
    }

    class Mage : Player
    {
        public Mage() : base(PlayerType.Mage)
        {
            //type = PlayerType.Mage;

            SetInfo(50, 15);
        }
    }
}

1. PlayerType 열거형 (Enum)

public enum PlayerType
{
    None = 0,
    Knight = 1,
    Archer = 2,
    Mage = 3
}
  • PlayerType 열거형(enum)은 플레이어의 직업을 정의하는 역할을 한다.
  • None = 0 : 기본값이며, 특정한 직업이 정해지지 않은 상태를 의미한다.
  • Knight = 1, Archer = 2, Mage = 3 : 각각 전사, 궁수, 마법사 직업을 나타낸다.
  • 이렇게 숫자로 지정하는 이유는 직업을 구별할 때 직관적이고, switch-case 문 등에서 유용하게 활용할 수 있기 때문이다.

2. Player 클래스 (부모 클래스)

class Player
{
    protected PlayerType _type = PlayerType.None;
    protected int hp = 0;
    protected int attack = 0;
  • Player 클래스는 공통된 플레이어 속성을 가지며, Knight, Archer, Mage 같은 직업 클래스들이 이를 상속받는다.
  • protected 접근 제어자를 사용하여 상속받은 클래스들에서만 접근할 수 있도록 했다.
  • _type : 플레이어의 직업을 저장하는 변수. 기본값은 PlayerType.None이다.
  • hp : 체력을 저장하는 변수.
  • attack : 공격력을 저장하는 변수.

3. Player 생성자 (외부에서 직접 생성 불가능)

protected Player(PlayerType type)
{
    _type = type;
}
  • protected 생성자이므로 외부에서 직접 Player 객체를 생성할 수 없다.
  • 상속받은 클래스(Knight, Archer, Mage)에서만 Player 생성자를 호출할 수 있다.
  • this._type = type;을 통해 플레이어의 직업을 설정한다.

💡 이유

  • Player 클래스는 직업별 공통 기능만 정의하는 역할을 하며, 직접 인스턴스화할 필요가 없기 때문이다.
  • 대신 Knight, Archer, Mage 같은 자식 클래스에서만 객체를 생성할 수 있도록 설계한 것이다.

4. SetInfo 메서드 (체력과 공격력 설정)

public void SetInfo(int hp, int attack)
{
    this.hp = hp;
    this.attack = attack;
}
  • hpattack 값을 설정하는 메서드로, 자식 클래스(Knight, Archer, Mage)에서 생성자를 통해 호출된다.
  • this.hp : 현재 객체의 체력(hp)을 설정.
  • this.attack : 현재 객체의 공격력(attack)을 설정.

5. 정보 조회 메서드 (Getter)

public int GetHP() { return hp; }
public int GetAttack() { return attack; }
public PlayerType GetPlayerType() { return _type; }
  • GetHP() : 플레이어의 체력을 반환.
  • GetAttack() : 플레이어의 공격력을 반환.
  • GetPlayerType() : 플레이어의 직업을 반환.

6. 사망 여부 체크 (IsDead 메서드)

public bool IsDead() { return hp <= 0; }
  • hp가 0 이하이면 true를 반환하여 사망했음을 나타낸다.

7. 데미지 처리 (OnDamaged 메서드)

public void OnDamaged(int damage)
{
    hp -= damage;
    if (hp < 0)
    {
        hp = 0;
    }
}
  • 공격을 받으면 체력이 감소한다.
  • 체력이 0 이하로 떨어지지 않도록, 최소값을 0으로 설정한다.

8. Knight 클래스 (전사)

class Knight : Player
{
    public Knight() : base(PlayerType.Knight)
    {
        SetInfo(100, 10);
    }
}
  • Knight 클래스는 Player 클래스를 상속받는다.
  • 생성자에서 base(PlayerType.Knight)를 호출하여 Player 생성자를 실행하고, 직업을 Knight로 설정한다.
  • SetInfo(100, 10)을 호출하여 체력 100, 공격력 10으로 초기화한다.

9. Archer 클래스 (궁수)

class Archer : Player
{
    public Archer() : base(PlayerType.Archer)
    {
        SetInfo(75, 12);
    }
}
  • Archer 클래스는 Player 클래스를 상속받는다.
  • base(PlayerType.Archer)를 호출하여 Player 생성자를 실행하고, 직업을 Archer로 설정한다.
  • SetInfo(75, 12)을 호출하여 체력 75, 공격력 12로 초기화한다.

10. Mage 클래스 (마법사)

class Mage : Player
{
    public Mage() : base(PlayerType.Mage)
    {
        SetInfo(50, 15);
    }
}
  • Mage 클래스는 Player 클래스를 상속받는다.
  • base(PlayerType.Mage)를 호출하여 Player 생성자를 실행하고, 직업을 Mage로 설정한다.
  • SetInfo(50, 15)을 호출하여 체력 50, 공격력 15로 초기화한다.

🔥 보충 설명

💡 C++과의 차이점

  • C++에서는 #include를 사용하여 클래스 파일을 가져와야 하지만, C#에서는 같은 네임스페이스(namespace CSharp) 안에 있는 클래스들은 자동으로 인식된다.
  • C++에서는 protected 생성자를 가진 클래스를 외부에서 생성할 수 없도록 하는 경우 friend class를 사용할 수도 있지만, C#에서는 해당 방식 없이 상속을 활용한다.

💡 객체 생성 예시

Player player = new Player();  // ❌ 불가능 (protected 생성자)
Knight knight = new Knight();  // ✅ 가능
Archer archer = new Archer();  // ✅ 가능
Mage mage = new Mage();        // ✅ 가능
  • Player 객체를 직접 생성할 수 없으며, Knight, Archer, Mage만 생성할 수 있도록 설계했다.

---😊

profile
李家네_공부방

0개의 댓글