简体   繁体   English

如何将 ToString() 方法覆盖到 output 列表?

[英]How can I override the ToString() method to output a list?

Is it possible to override the ToString() method to output a list?是否可以将 ToString() 方法覆盖为 output 列表? Here's my code这是我的代码

    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]));

        }
    }
}

How can I override ToString() to output all the children in a list?如何将 ToString() 覆盖为 output 列表中的所有孩子?

Looks like you're already overriding the ToString() method on your class... so... use a StringBuilder and a for/foreach loop to construct your output string?看起来您已经在 class 上覆盖了 ToString() 方法......所以......使用 StringBuilder 和 for/foreach 循环来构造 output 字符串?

Perhaps you would could output the children in a foreach loop, like this:也许你可以 output 在 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();
}

I haven't compiled this, so please correct me if I made a mistake.我没有编译这个,所以如果我犯了错误,请纠正我。 [edit] compiled now... :) [编辑] 现在编译... :)

To output a list?output一个清单? As in, return a list?如,返回一个列表? No. Two reasons:不,有两个原因:

  1. You can't change the return type of an overridden method.您不能更改被覆盖方法的返回类型。
  2. ToString() implies that it returns a string. ToString()意味着它返回一个字符串。 Returning anything else would be highly unintuitive and invites support problems with the code.返回任何其他内容都非常不直观,并且会导致代码出现支持问题。

You can't change the return type of an overriden method.不能更改被覆盖方法的返回类型。

You will need to write your own method that returns a list:您将需要编写自己的返回列表的方法:

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

Yes:是的:

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

You can override the ToString as you have done already.您可以像已经完成的那样覆盖ToString You could iterate through the elements of the list to output the string of your desire.您可以遍历列表的元素到 output 您想要的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM