我試圖將自定義類型的'數量'添加到int變量'_StoredStock',但我在如何做到這一點上沒有拋出轉換異常的空白。任何幫助,將不勝感激。我已經添加了相關的類,以便您瞭解我正在嘗試做什麼。將自定義類型添加到變量類型int?
class DiscreteStockQuantity : StockQuantity
{
private int _Quantity;
public int Quantity
{
get { return _Quantity; }
}
public DiscreteStockQuantity (int quantity)
{
_Quantity = quantity;
}
}
class DiscreteStock : Stock
{
private int _StoredStock;
public int StoredStock
{
get { return _StoredStock; }
}
private int _ShelfStock;
public int ShelfStock
{
get { return _ShelfStock; }
}
private double _WeightPerItem;
public double WeightPerItem
{
get { return _WeightPerItem; }
}
public DiscreteStock(Line line, double weightPerItem) : base(line)
{
_ShelfStock = 0;
_StoredStock = 0;
_WeightPerItem = weightPerItem;
}
public override void AdjustStock(StockQuantity quantity)
{
if (quantity is DiscreteStockQuantity)
{
}
}
abstract class Stock
{
private Line _Line;
public Line Line
{
get { return _Line; }
}
public Stock (Line line)
{
_Line = line;
}
public abstract void AdjustStock(StockQuantity quantity);
public abstract bool Purchase(StockQuantity quantity, out Item item);
public abstract void Restock();
}
給出'DiscreteStockQuantity'的代碼 –
在'if'裏面你知道它是DSQ,爲什麼不直接調用返回int值的方法或調用添加int的方法?另一種選擇是重寫DSQ –
@RafałCzabajWTF中的'+'運算符? – leppie