我是C#的新手,请原谅我所知的差距。在我的代码中,我有两种类型的食物Fresh和Regular。 确定成本时,新鲜项目使用与常规不同的FindCost方法。
我无法解决如何为Fresh Items调用FindCost()方法。
我想我需要以某种方式使用base关键字:
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;
}
}
}
任何帮助表示赞赏
答案 0 :(得分:0)
您当前的代码正确地调用了正确的FindCost()
方法,但您已经以糟糕的方式定义了类,并且使用new
关键字隐藏可能会使其调用错误的方法。
您应该GroceryItem
abstract
并让您的两个类直接从此继承。然后GroceryItem
可以使用abstract
FindCost()
方法。此外,如果您隐藏了new
的方法,那么您可能做错了什么。您应该发现这在调用FindCost()
时消除了所有不确定性。
另外,您应该使用decimal
而不是double
进行财务计算,因为double
并不总是准确地代表货币价值。
以下是改进的课程:
void Main()
{
List<GroceryItem> invoiceArray = new List<GroceryItem>()
{
new FreshItem() { Name = "Rump Steak", Price = 11.99m, Weight = 0.8 },
new PurchasedItem() { Name = "Cream", Price = 2.2m, Quantity = 1, Weight = 0.6 },
};
foreach (GroceryItem checkoutItem in invoiceArray)
{
if (checkoutItem is PurchasedItem purchasedItem)
{
Console.WriteLine($"Condition: {checkoutItem.Condition}; Quantity: {purchasedItem.Quantity}; Weight: {checkoutItem.Weight}kg");
}
else
{
Console.WriteLine($"Condition: {checkoutItem.Condition}; Weight: {checkoutItem.Weight}kg");
}
}
Console.ReadLine();
}
public abstract class GroceryItem
{
public string Name { get; set; }
public abstract string Condition { get; }
public double Weight { get; set; }
public decimal Price { get; set; }
public GroceryItem() { }
public GroceryItem(string name, decimal price)
{
this.Name = name;
this.Price = price;
}
public abstract decimal FindCost();
}
public class PurchasedItem : GroceryItem
{
public int Quantity { get; set; }
public override string Condition { get { return "Regular"; } }
public override decimal FindCost()
{
decimal cost = Quantity * Price * 1.1m; //1.1m is GST
return cost;
}
}
public class FreshItem : GroceryItem
{
public override string Condition { get { return "Fresh"; } }
public override decimal FindCost()
{
decimal cost = (decimal)Weight * Price;
return cost;
}
}
答案 1 :(得分:0)
使用PurchasedItem类型的引用设置Weight时: CheckoutItem4.Weight = .8; 您设置在PurchasedItem类中声明的字段Weight,但FindCost方法使用未初始化的类FreshItem中定义的字段Weight。所以,只需删除de line: public new double Weight {get;组; } // kg 。您无需重新定义此字段。