C#

클래스 연습(벌처)

s0002 2023. 1. 4. 17:53
using System;
using System.Collections.Generic;
using System.Text;

namespace Study03
{
    class App
    {
   
        //생성자
        public App()
        {
            Vulture vulture = new Vulture();

            Console.WriteLine();

            SpiderMine mine0 = new SpiderMine();
            SpiderMine mine1 = new SpiderMine();
            SpiderMine mine2 = new SpiderMine();

            Console.WriteLine();

            vulture.mine0 = mine0;
            vulture.mine1 = mine1;
            vulture.mine2 = mine2;

            vulture.Move();

            Console.WriteLine();

            vulture.InstallMine();
            vulture.InstallMine();
            vulture.InstallMine();
            vulture.InstallMine();

            Console.WriteLine();
        }
    } 
}
using System;
using System.Collections.Generic;
using System.Text;

namespace Study03
{
    class Vulture
    {
        const int MAX_MINE_COUNT = 3;
        int mineCount = MAX_MINE_COUNT;

        public float moveSpeed = 3.126f;

        public SpiderMine mine0;
        public SpiderMine mine1;
        public SpiderMine mine2;

        //생성자
        public Vulture()
        {
            Console.WriteLine(moveSpeed);
        }
        public void Move()
        {
            Console.WriteLine("이동했습니다");
        }
        public void InstallMine() 
        {
            if (mine0 != null)
            {
                mine0 = null;
                mineCount = mineCount - 1;
                Console.WriteLine("mine0을 설치했습니다({0}/{1})",mineCount,MAX_MINE_COUNT);
            }
            else if(mine1 != null)
            {
                mine1 = null;
                mineCount = mineCount - 1;
                Console.WriteLine("mine1을 설치했습니다({0}/{1})", mineCount, MAX_MINE_COUNT);
            }
            else if (mine2 != null)
            {
                mine2 = null;
                mineCount = mineCount - 1;
                Console.WriteLine("mine2를 설치했습니다({0}/{1})", mineCount, MAX_MINE_COUNT);
            }
            else
            {
                Console.WriteLine("마인이 없습니다");
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Text;

namespace Study03
{

    class SpiderMine
    {
        const int MAX_MINE_COUNT = 3;
        static int mineCount = 0;

        //생성자
        public SpiderMine()
        {
            Console.Write("SpiderMine이 생성되었습니다");
            CountMine();
        }
        void CountMine()
        {
            mineCount++;
            Console.WriteLine("({0}/{1})",mineCount,MAX_MINE_COUNT);
        }

    }
}

'C#' 카테고리의 다른 글

클래스 연습2  (0) 2023.01.04
클래스 연습1  (0) 2023.01.04
메서드 연습4  (0) 2023.01.04
메서드 연습3  (0) 2023.01.03
메서드 연습2  (0) 2023.01.03