C#/수업내용
[ C# 8일차 ] Generic 일반화 Class
by 왹져박사
2023. 1. 10.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace Study09
{
class App
{
//생성자
public App()
{
Console.WriteLine("App");
Inventory<Weapon> inventory = new Inventory<Weapon>(10);
inventory.AddItem(new Weapon());
Inventory<Armor> inventory1 = new Inventory<Armor>(10);
inventory1.AddItem(new Armor());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study09
{
class Inventory<T>
{
public T[] equipments;
int index = 0;
//생성자
public Inventory(int capacity)
{
//배열 초기화(인스턴스화)
this.equipments = new T[capacity];
}
public void AddItem(T item)
{
this.equipments[this.index++] = item;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study09
{
class Weapon
{
//무기 관리를 위한 배열
//생성자
public Weapon()
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study09
{
class Armor
{
//방어구 관리를 위한 배열
//생성자
public Armor()
{
}
}
}