嘿伙计们,我不确定当我从主代码启动控制台时,它会打印两次不确定的项目,因为我根本没有循环方法:
FoodProducts FoodProd1 = new FoodProducts("FP001", "Meat", 15.99, 200, 100, "Australia");
FoodProducts FoodProd2 = new FoodProducts("FP002", "Bread", 2.99, 150, 50, "Italy");
FoodProd1.Print();
FoodProd2.Print();
class FoodProducts : Products
{
private string origin;
public FoodProducts(string id, string name, double price, int soldCount, int stockCount, string origin)
: base(id, name, price, soldCount, stockCount)
{
this.origin = origin;
//Need to find out why this code prints both lines and not in single line and why it starts from Product 2 when it is printed on the console
PrintOrigin();
}
private string Origin
{
get { return origin; }
set { origin = value; }
}
public void PrintOrigin()
{
base.Print();
Console.WriteLine("Origin: {0}", this.Origin);
}
从评论更新
Print()
基类中定义的方法:
public void Print() {
Console.WriteLine("Product ID: {0}", this.id);
Console.WriteLine("Product Name: {0}", this.name);
Console.WriteLine("Prodcut Price: {0}", this.price);
Console.WriteLine("Sold Counter: {0}", this.soldCount);
Console.WriteLine("Stock Count: {0}", this.stockCount);
Console.WriteLine();
Console.ReadKey();
}