C#/수업 내용

[C#] class 상속(parent, child), virtual, override

JSH1 2021. 8. 29. 23:41
class Program
{
    public static int mineral = 1000;

    public enum eBuildingType
    {
        CommandCenter,
        Barracks
    }

    public enum eUnitType
    {
        Marine,
        Medic
    }

    static void Main(string[] args)
    {
        CommandCenter commandCenter = new CommandCenter(0, 0);
        Minerals minerals = new Minerals(3, 2, 1500);
        SCV scv = new SCV(0, 0);
        Console.WriteLine();
        
        Building barracks = scv.BuildStructure(1, 2, eBuildingType.Barracks);
        Unit marine = barracks.UnitTrain(barracks.locationX, barracks.locationY, eUnitType.Marine);
        marine.Attack();
    }
}
class CommandCenter
{
    public int x;
    public int y;

    //생성자
    public CommandCenter(int x, int y)
    {
        Console.WriteLine("{0}가 (x:{1} ,y:{2}) 위치에 생성 되었습니다.", this, x, y);
        this.x = x;
        this.y = y;
    }
}
class Minerals
{
    public int deposits;
    public int x;
    public int y;

    //생성자
    public Minerals(int x, int y, int deposits)
    {
        this.x = x;
        this.y = y;
        this.deposits = deposits;
        Console.WriteLine("{0}({1})가 (x:{2} ,y:{3}) 위치에 생성 되었습니다.", this, this.deposits, x, y);
    }
}
class SCV
{
    private int x;
    private int y;

    //생성자
    public SCV(int x, int y)
    {
        Console.WriteLine("{0}가 (x:{1} ,y:{2}) 위치에 생성 되었습니다.", this, x, y);
        this.x = x;
        this.y = y;
    }

    public void Move(int x, int y)
    {
        Console.WriteLine("이동 명령을 내립니다.");
        Console.WriteLine("목표 위치: ({0},{1})", x, y);
        Console.WriteLine("이동중: ({0},{1})", this.x, this.y);
        while (this.x != x || this.y != y)
        {
            if (this.x < x)
            {
                this.x++;
            }
            else if (this.x > x)
            {
                this.x--;
            }

            else if (this.y < y)
            {
                this.y++;
            }
            else if (this.y > y)
            {
                this.y--;
            }
            Console.WriteLine("이동중: ({0},{1})", this.x, this.y);
        }
        Console.WriteLine("이동 완료");
    }

    public Building BuildStructure(int x, int y, Program.eBuildingType buildingType)
    {
        Console.WriteLine("┌ 건설 명령을 내립니다.");

        switch (buildingType)
        {
            case Program.eBuildingType.CommandCenter:
                return null;

            case Program.eBuildingType.Barracks:
                if (Program.mineral < 150)
                {
                    Console.WriteLine("└ 미네랄이 부족합니다.");
                    return null;
                }
                else
                {
                    Move(x, y);
                    Console.WriteLine("└ 배럭 건설을 시작합니다.");
                    return new Barracks(x, y);
                }
        }
        return null;
    }
}
class Building
{
    public int locationX;
    public int locationY;

    //생성자
    public Building()
    {
        
    }

    public virtual Unit UnitTrain(int x, int y, Program.eUnitType unitType) //다른 모든 테란 유닛은 제작 Build인 반면, 병영 유닛은 훈련 Train으로 달리 표기함
    {
        return new Marine(x, y);
    }
}
class Barracks : Building
{
    //생성자
    public Barracks(int x, int y)
    {
        Console.WriteLine("{0}이 (x:{1} ,y:{2}) 위치에 생성 되었습니다.", this, x, y);
        base.locationX = x;
        base.locationY = y;
    }
    
    public override Unit UnitTrain(int x, int y, Program.eUnitType unitType) //다른 모든 테란 유닛은 제작 Build인 반면, 병영 유닛은 훈련 Train으로 달리 표기함
    {
        return new Marine(x, y);
    }
}
class Unit
{
    //생성자
    public Unit()
    {

    }

    public virtual void Attack()
    {
        Console.WriteLine("parent 공격 메서드");
    }
}
class Marine : Unit
{
    //생성자
    public Marine(int x, int y)
    {
        Console.WriteLine("{0}이 (x:{1} ,y:{2}) 위치에 생성 되었습니다.", this, x, y);
    }

    public override void Attack()
    {
        Console.WriteLine("child 공격 메서드");
    }
}

'C# > 수업 내용' 카테고리의 다른 글

[C#] 구조체 struct  (0) 2021.08.30
[C#] 2021-08-30  (0) 2021.08.30
[C#] 2021-08-27 오후  (0) 2021.08.27
[C#] class virtual override  (0) 2021.08.27
[C#] 2021-08-26 오후  (0) 2021.08.26