2017-09-04 142 views
-1

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

} 

任何幫助讚賞

+0

您可能希望將其縮小爲僅有相關代碼。 – juharr

+0

嘗試避免'ArrayList' - 使用'List '來代替。 – Enigmativity

+0

白色空間傷害我 – anomeric

回答

0

您當前的代碼是否正確調用正確的FindCost()方法,但你在定義了類不好的方法,並與new關鍵字隱藏可以使它調用錯誤的方法。

您應該製作GroceryItemabstract並讓您的兩個類直接繼承此類。然後GroceryItem可以有一個abstractFindCost()方法。另外,如果你隱藏了一個方法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; 
    } 
} 
+0

非常感謝你,我會通過每個職位。我發佈了所有代碼,因爲我擔心我會忽略相關的內容。 – bronxsystem

+0

我嘗試使用你的代碼,但它會得到大量的錯誤。我會通過一些教程,並學習更多關於繼承= /謝謝你,雖然只是棘手的不知道所有的基本面。 [錯誤圖像](https://image.ibb.co/hF7MKF/errors.jpg) – bronxsystem

+0

@bronxsystem - 你應該能夠從我的代碼工作,讓你的代碼工作。由於我所做的更改 - 你應該使用它 - 它使你現有的代碼失敗,但調試這些問題是你應該學習的技能。爲了幫助我更新我的答案中的代碼,包括如何使用它。讓我知道如果你沒有使用C#7作爲'if(checkoutItem is PurchasedItem purchasedItem)''在早期版本的C#中不起作用。 – Enigmativity

0

當設置重量連類型PurchasedItem的參考: CheckoutItem4.Weight = 0.8; 您設置了在類PurchasedItem中聲明的字段Weight,但方法FindCost使用FreshItem類中定義的字段Weight,該字段未初始化。所以,只需刪除德行:public new double Weight {get;組; } // kg。您不需要重新定義此字段。

+0

當我使用你的方式時,它可以工作,但是這項任務要求我在新鮮的課程中使用Weight成員,Weight只能用在FreshItem課程中 我會在發佈的建議線程感謝所有 – bronxsystem

+0

我從來沒有見過這種使用之前'code' public override string Condition {get {return「Regular」; }} 我想了解更多關於它的具體術語嗎?我將需要審查繼承,抽象和重寫等,但有沒有一個代碼行的名稱? – bronxsystem