2011-06-13 29 views
2

是否可以重寫ToString()方法來輸出列表? 這裏是我的代碼如何覆蓋ToString()方法來輸出列表?

namespace CircularClass 
{ 
    class InfinitelyCircular 
    { 

     public class RatsInAShip 
     { 
      public string RatBreed; 
      public string RatWeight; 
      public string RatAge; 
      public bool HasChildren; 
      public List<RatsInAShip> RatChildren; 
      public override string ToString() 
      { 
       return String.Format("RatBreed:{0}\n RatWeight:{1}\n RatAge:{2}\n HasChildren:{3}",RatBreed, RatWeight, RatAge, HasChildren); 
      } 
     } 

     static void Main(string[] args) 
     { 
      var mu = new RatsInAShip() 
         { 
          RatAge = "100", 
          RatBreed = "RexRat", 
          RatWeight = "200", 
          HasChildren = true, 
          RatChildren = new List<RatsInAShip>() 
         }; 

      for (var i = 0; i < 3; i++) 
      { 
       var cu = new RatsInAShip() 
          { 
           RatAge = "100", 
           RatBreed = "DwarfRat"+i, 
           RatWeight = "200", 
           HasChildren = false 
          }; 
       mu.RatChildren.Add(cu); 
      } 

      //Output 
       Console.WriteLine(String.Format("{0}, {1}", mu.RatBreed, mu.RatChildren[0])); 

     } 
    } 
} 

我怎麼可以重寫的ToString()輸出所有的孩子在一個列表?

+0

你所說的「名單」是什麼意思?你在談論List類嗎?或者你只是指'String'中的逗號分隔列表? – 2011-06-13 13:20:54

+0

你不能,'ToString()'和任何重寫的方法只能返回與父類相同的類型(在本例中爲'string')。你的意思是你想要在一條單獨的線上爲每個孩子輸出一個字符串嗎? – 2011-06-13 13:21:02

+0

此[鏈接](http://msdn.microsoft.com/en-us/library/ms173154(VS.80)的.aspx)可以幫助 – robasta 2011-06-13 13:24:14

回答

3

也許你可以輸出的孩子在foreach循環中,像這樣:

public override string ToString() 
{ 
    StringBuilder sb = new StringBuilder(); 
    sb.AppendFormat("RatBreed:{0}\n RatWeight:{1}\n RatAge:{2}\n HasChildren:{3}\n",RatBreed, RatWeight, RatAge, HasChildren); 
    foreach (RatsInAShip rat in RatChildren) 
    { 
     sb.AppendFormat("\tRat Child: {0}\n", rat); 
    } 
    return sb.ToString(); 
} 

我沒有編這一點,所以請糾正我,如果我犯了一個錯誤。 [編輯]現在編... :)

+0

注:ABHI,當我運行它,我得到一個異常,告訴你RatChildren變量沒有被設置爲一個對象的實例。你將不得不修改你的類的其餘部分,以確保列表被實例化。但是這需要對程序進行其他與「ToString」無關的更改。 – 2011-06-13 13:31:26

+0

因此,如果您將主要功能中的最後一行設置爲: 控制檯。的WriteLine(畝); 此外,我添加了一個初始化程序到RatsInAShip類定義,如下所示: public List RatChildren = new List (); 一旦做到這一點,你會看到正確的結果是這樣的: RatBreed:RexRat RatWeight:200 RatAge:100 HasChildren:真 鼠兒童:RatBreed:DwarfRat0 RatWeight:200 RatAge:100個 HasChildren:假 鼠兒童:RatBreed:DwarfRat1 RatWeight:200 RatAge:100個 HasChildren:假 鼠兒童:RatBreed:DwarfRat2 RatWeight:200 RatAge:100個 HasChildren:假 – 2011-06-13 13:33:23

1

不能變化的覆蓋方法的返回類型。

你需要編寫自己的方法,該方法返回一個列表:

public IList<string> ToList() 
{ 
    // your code that returns a list of strings 
} 
4

看起來你已經覆蓋toString()方法類...所以...使用StringBuilder和一個for/foreach循環來構造你的輸出字符串?

1

是:

public override string ToString() 
{ 
    string result = ""; 
    if(RatChildren != null) 
     foreach(var x in RatChildren) 
     result += String.Format("RatBreed:{0}\n RatWeight:{1}\n RatAge:{2}\n HasChildren:{3}", x.RatBreed, x.RatWeight, x.RatAge, x.HasChildren); 

    return result; 
} 
+1

鋸的編輯,你也應該得到它做一個換行符,或附加到字符串時的其他分隔符。另外考慮使用'StringBuilder'。 – 2011-06-13 13:23:14

+0

Grrr,我提供了獲得+2的傢伙的建議的代碼,並且我得到了-1?撤消請... – Nathan 2011-06-13 13:23:20

+0

我做了-1,當你有不正確的代碼。我現在撤消了我的-1。 – 2011-06-13 13:25:57

1

輸出列表?如在,返回一個列表? No.兩個原因:

  1. 您無法更改重寫方法的返回類型。
  2. ToString()意味着它返回一個字符串。返回其他任何東西都會非常不直觀,並會引起代碼的支持問題。
0

您可以覆蓋ToString,因爲您已經完成了。你可以遍歷列表中的元素來輸出你想要的字符串。