유니티3D 프로그래밍
C# 2주차 3일 수업 과제 : Stack (21.03.17) 본문
Stack
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study01
{
class App
{
public App()
{
Console.WriteLine("App");
Stack<int> num = new Stack<int>();
for(int i = 0; i < 10; i++)
{
num.Push(i);
}
foreach(int count in num)
{
Console.Write("{0} ",count);
}
Console.WriteLine();
int num1 = num.Pop();
foreach (int count in num)
{
Console.Write("{0} ", count);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study01
{
class App
{
public App()
{
Console.WriteLine("App");
Stack<Item> items = new Stack<Item>();
Item item = new Item("체력포션1");
Item item1 = new Item("체력포션2");
Item item2 = new Item("체력포션3");
Item item3 = new Item("체력포션4");
Item item4 = new Item("체력포션5");
items.Push(item);
items.Push(item1);
items.Push(item2);
items.Push(item3);
items.Push(item4);
foreach (Item count in items)
{
Console.WriteLine("{0} ", count.GetName());
}
Console.WriteLine();
items.Pop();
foreach (Item count in items)
{
Console.WriteLine("{0} ", count.GetName());
}
Console.WriteLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study01
{
public class Item
{
private string name;
public Item(string name)
{
this.name = name;
}
public string GetName()
{
return this.name;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study01
{
class App
{
public App()
{
Console.WriteLine("App");
Inventory inven = new Inventory();
Item item = new Item("체력포션");
Item item1 = new Item("마력포션");
Item item2 = new Item("표창");
Item item3 = new Item("폭탄");
inven.SetItemInventory(item);
inven.SetItemInventory(item1);
inven.SetItemInventory(item2);
inven.SetItemInventory(item3);
//인벤토리 내의 아이템의 이름 불러오기
Console.WriteLine(inven.GetItem(0).GetName());
Console.WriteLine(inven.GetItem(1).GetName());
Console.WriteLine(inven.GetItem(2).GetName());
Console.WriteLine(inven.GetItem(3).GetName());
//인벤토리 내의 아이템 사용하기
inven.GetItem(0).Use();
inven.GetItem(1).Use();
inven.GetItem(2).Use();
inven.GetItem(3).Use();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study01
{
public class Inventory
{
List<Item> item = new List<Item>();
public Inventory()
{
}
//인벤토리에 아이템 추가
public void SetItemInventory(Item item)
{
this.item.Add(item);
}
//인벤토리에 있는 아이템 불러오기
public Item GetItem(int index)
{
return item[index];
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study01
{
public class Item : Inventory, IUse
{
private string name;
public Item(string name)
{
this.name = name;
}
public string GetName()
{
return this.name;
}
public void Use()
{
Console.WriteLine("{0} 아이템을 사용합니다.", this.name);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study01
{
interface IUse
{
void Use();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study01
{
class App
{
public App()
{
Console.WriteLine("App");
Item item = new Item("장검");
Item item1 = new Item("단검");
//인벤토리에 아이템 추가
Inventory inventory = new Inventory(item);
//인벤토리의 아이템 출력
Console.WriteLine(inventory.ItemName.GetName());
//인벤토리에 아이템 변경
inventory.ItemName = item1;
//인벤토리의 아이템 출력
Console.WriteLine(inventory.ItemName.GetName());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study01
{
public class Inventory
{
private Item itemName;
public Item ItemName
{
get
{
return itemName;
}
set
{
itemName = value;
}
}
public Inventory(Item itemName)
{
this.itemName = itemName;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study01
{
public class Item
{
private string name;
public Item(string name)
{
this.name = name;
}
public string GetName()
{
return this.name;
}
}
}
'C# > 수업과제' 카테고리의 다른 글
C# 3주차 1일 수업 과제 : Delegate, Action, Callback (21.03.22) (0) | 2021.03.22 |
---|---|
C# 2주차 5일 수업 내용 및 과제 : 2차원 배열 및 2048 프로그램(21.03.19) (0) | 2021.03.19 |
C# 2주차 2일 수업 과제 (21.03.16) (0) | 2021.03.16 |
C# 2주차 1일 수업 과제 (21.03.15) (0) | 2021.03.15 |
C# 1주차 5일 연습 과제 (21.03.12) (0) | 2021.03.12 |