我是C#的新手,所以請原諒我的知識空白。在我的代碼中,我有兩種新鮮和常規的食物。 確定成本時新鮮料品使用與普通不同的FindCost方法。C# - 調用覆蓋方法
我在解決如何爲Fresh項目調用FindCost()方法時遇到問題。
我想我需要使用基本關鍵字莫名其妙S:
namespace Groceries
{
class Program
{
static void Main(string[] args)
{
PurchasedItem CheckoutItem4 = new FreshItem();
CheckoutItem4.Name = "Rump Steak";
CheckoutItem4.Condition = "Fresh";
CheckoutItem4.Price = 11.99;
CheckoutItem4.Weight = .8;
ArrayList invoiceArray = new ArrayList();
invoiceArray.Add(CheckoutItem1);
foreach (PurchasedItem checkoutItem in invoiceArray)
{
Console.WriteLine($"Quantity: {checkoutItem.Quantity} Weight: {checkoutItem.Weight}kg ");
}
Console.ReadLine();
}
}
public class GroceryItem
{
public string Name { get; set; }
public string Condition { get; set; }
public double Price { get; set; }
public GroceryItem()
{
}
public GroceryItem(string name, double price)
{
this.Name = name;
this.Price = price;
}
public static void Invoice()
{
Console.WriteLine();
}
}
public class PurchasedItem : GroceryItem
{
public int Quantity { get; set; }
public double Weight { get; internal set; }
public virtual double FindCost()
{
double cost = Quantity * Price * 1.1; //1.1 is GST
return cost;
}
}
public class FreshItem : PurchasedItem
{
public new double Weight { get; set; } //kg
public override double FindCost()
{
double cost = Weight * Price;
return cost;
}
}
}
任何幫助讚賞
您可能希望將其縮小爲僅有相關代碼。 – juharr
嘗試避免'ArrayList' - 使用'List'來代替。 –
Enigmativity
白色空間傷害我 – anomeric