2012-10-21 65 views
0

我目前正在使用抽象類和虛擬方法。我正在做一個窗口窗體的小項目,列出兩個類TreesTomatoes。結果顯示在兩個文本框中。 TextBox1顯示名稱,庫存數量和每件物品的價格。 TextBox2顯示每個單個工廠的總價格。我試圖添加一個方法來顯示所有工廠的總數,並在TextBox2中顯示。抽象類和虛擬方法 - 顯示總數

代碼

namespace nursery_plant 
    { 
     public partial class Form1 : Form 
     { 
      public Form1() 
      { 
       InitializeComponent(); 
      } 

     public abstract class Plants 
     { 
      public string the_name; 
      public double num_stock; 
      public double price_peritem; 
      public double total_item_value; 

      public Plants(string new_name, int new_stock, double new_price) 
      { 
       the_name = new_name; 
       num_stock = new_stock; 
       price_peritem = new_price; 
      } 

      public override string ToString() 
      { 
       return ""; 
      } 

      public virtual double Get_Value() 
      { 
       double s = 0; 
       return s; 
      } 


     } 

     public class Trees : Plants 
     { 
      double tree_height; 
      public Trees(string new_name, int new_stock, double new_price, double new_height) 
       : base(new_name, new_stock, new_price) 
      { 
       tree_height = new_height; 
       total_item_value = num_stock * price_peritem; 
      } 

      public override string ToString() 
      { 
       string s = "Tree" + "  " + num_stock + "  " + the_name + "  " + price_peritem + " " + tree_height ; 
       return s; 
      } 

      public override double Get_Value() 
      { 
       total_item_value = num_stock * price_peritem; 
       return total_item_value; 
      } 

     } 

     public class Tomatoes : Plants 
     { 

      string sizeoftomato; 
      int tomatoesinpat; 

      public Tomatoes(string new_name, int new_stock, double new_price, int tomatoes_perplat, string tomatoes_size) 
       : base(new_name, new_stock, new_price) 
      { 
       tomatoesinpat = tomatoes_perplat; 
       sizeoftomato = tomatoes_size; 


       total_item_value = num_stock * price_peritem; 


      } 

      public override string ToString() 
      { 
       string s = "Tomatoes" + "  " + num_stock + "  " + the_name + "  " + price_peritem; 
       return s; 
      } 

      public override double Get_Value() 
      { 

       total_item_value = price_peritem; 
       return total_item_value; 
      } 
     } 




     public void Report() 
     { 

      Trees trees1 = new Trees("Tree", 3, 14.40, 2); 
      const string format = "{0,-26} {1,-25} {2,-25} {3,-25}"; 
      string trees1_result = String.Format(format, trees1.the_name, "Oak", trees1.num_stock, trees1.price_peritem);    
      textBox1.AppendText(trees1_result + Environment.NewLine); 
      textBox2.AppendText(trees1.Get_Value() + Environment.NewLine); 

      Tomatoes tomatoes1 = new Tomatoes("Tomatoe", 30, 10, 12, "Large"); 
      const string format2 = "{0,-26} {1,-25} {2,-25} {3,-25}"; 
      string tomatoes1_result = String.Format(format2, tomatoes1.the_name, "Big Boy", tomatoes1.num_stock, tomatoes1.price_peritem); 
      textBox1.AppendText(tomatoes1_result + Environment.NewLine); 
      textBox2.AppendText(tomatoes1.Get_Value() + Environment.NewLine); 

     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      Report(); 
     } 


    } 
} 

回答

2

你就不能使用這樣的事情?

textBox2.AppendText(trees1.total_item_value + tomatoes1.total_item_value); 

另外,我注意到有一些共享的邏輯你在重複。這條線,例如

total_item_value = num_stock * price_peritem; 

可以去在抽象基類的構造函數。