我是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