CSharp · Console
using System;
using System.Collections.ObjectModel;
public class InventoryItem
{
public string Name
{
get;
set;
}
}
public class SuperInventoryItem : InventoryItem
{
}
public class InventoriItemFabric
{
public static InventoryItem Create()
{
return new InventoryItem();
}
}
public class SuperInventoriItemFabric
{
public static SuperInventoryItem Create()
{
return new SuperInventoryItem();
}
}
public class Inventory<T>
where T : InventoryItem
{
private ObservableCollection<T> _inventory = new ObservableCollection<T>();
public Inventory()
{
}
public void Add(T item)
{
_inventory.Add(item);
}
public int Count
{
get => _inventory.Count;
}
}
public class Program
{
public static void Main()
{
var c1 = new Inventory<InventoryItem>();
c1.Add(InventoriItemFabric.Create());
c1.Add(SuperInventoriItemFabric.Create());
var c2 = new Inventory<SuperInventoryItem>();
//c2.Add(InventoriItemFabric.Create()); // Компилятор так сделать не даст
c2.Add(SuperInventoriItemFabric.Create());
Console.WriteLine(c1.Count);
Console.WriteLine(c2.Count);
}
}