본문 바로가기
C#/수업내용

[C# 6일차] Method return 연습_Box에서 Item 획득

by 왹져박사 2023. 1. 6.
728x90

App Class

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

namespace Study06
{
    class App
    {
        //생성자
        public App()
        {
            Console.WriteLine("App 생성자");

            //뽑기상자에서 뽑으면 아이템이 나옴

            Box box = new Box();
            Item item = box.Open();
            Inventory inventory = new Inventory();
            inventory.AddItem(item);

        }
    }
}

 

Box Class

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

namespace Study06
{
    class Box
    {
        //생성자
        public Box()
        {
            Console.WriteLine("Box 생성");
        }
        public Item Open()
        {
            Console.WriteLine("{0}를 열었습니다.", this);
            return new Item();
        }
    }
}

 

 

Item Class

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

namespace Study06
{
    class Item
    {
        public string name = "item";
        //생성자
        public Item()
        {
            Console.WriteLine("Item 생성");
        }
    }
}

 

Inventory Class

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

namespace Study06
{
    class Inventory
    {
        Item item;
        //생성자
        public Inventory()
        {
            Console.WriteLine("Inventory 생성");
        }
        public void AddItem(Item item)
        {
            this.item = item;
            Console.WriteLine("{0}이 {1}에 저장되었습니다. ", item, this);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study06
{
    class Program
    {
        static void Main(string[] args)
        {
            new App();
        }
    }
}

 

실행결과

728x90