简体   繁体   English

在ToString覆盖中使用ToString时出错

[英]Error with using ToString in ToString override

I am new to coding and I have a problem with using ToString override. 我是编码新手,使用ToString覆盖时遇到问题。 When I try to use already overloaded class's ToString in other ToString override, I get this error: 当我尝试在其他ToString覆盖中使用已经重载的类的ToString时,出现此错误:

An object reference is required to non static field, method, or property Freight.ToString() 非静态字段,方法或属性Freight.ToString()需要对象引用

Please help! 请帮忙!

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;

 namespace TruckCmopany
 {
     class Truck
     {
         private string name;
         private double weightCapacity;
         private List<Freight> freights;

         public Truck(string name,double weightCapacity)
         {
              this.Name = name;
              this.WeightCapacity = weightCapacity;
              List<Freight> freights = new List<Freight>();
         }

         public string Name
         {
             get { return name; }
             set { name = value; }
         }

         public double WeightCapacity
         {
             get { return weightCapacity; }
             set { weightCapacity = value; }
         }

         public override string ToString()
         {
             StringBuilder sb = new StringBuilder();
             sb.Append(this.Name).Append(" - ");

             if (freights.Count==0)
             {
                 sb.Append("Nothing loaded");
             }
             else
             {
                 sb.Append(string.Join(", ", freights)).Append(Freight.ToString());
             }

             return sb.ToString();
         }

         public IReadOnlyCollection<Freight> Freights
         {
             get => freights.AsReadOnly();
         }

         public void AddFreight(Freight freight)
         {
         }
    }
}

To be able to call Freight.ToString() ToString() has to be a static function, ie, a function that belongs to the class itself that doesn't require any specific instance of the class to get invoked. 为了能够调用Freight.ToString() ToString()必须是静态函数,即,属于类本身的函数,不需要调用该类的任何特定实例。 However when you convert ToString to be static, you won't have access to the this modifier, since in a static method, there is no instance that you can refer to with this . 但是,当您将ToString转换为静态时,将无法访问this修饰符,因为在静态方法中,没有可以使用this引用的实例。 So you are going to have to pass an instance of Freight to your static function. 因此,您将必须将Freight的实例传递给静态函数。

You can then override ToString() and inside pass this to your static function. 然后,您可以覆盖toString()和内传递this对您的静态函数。 If you want to. 如果你想。

Read on Static Members: MSDN 阅读静态成员: MSDN

Read on Methods: MSDN 阅读方法: MSDN

Freight.ToString() is a call to a static method. Freight.ToString()是对静态方法的调用。 It confuses the compiler, because it tries to resolve the call to the non-static .ToString inherited from object . 它使编译器感到困惑,因为它尝试解析对从object继承的非静态.ToString的调用。

Since static classes cannot override members, if you really need a static ToString in your Freight class, you need to mark the method with new public . 由于静态类无法覆盖成员,因此如果在Freight类中确实需要静态ToString ,则需要使用new public标记该方法。


Well, I see that your Freight is not a static class. 好吧,我发现您的Freight不是静态类。

Then if I got you right, and you want a comma-separated string of freights, you need to simply do 然后,如果我说对了,并且您想用逗号分隔一连串的运费,则只需

sb.Append(string.Join(", ", freights));

It will call .ToString for each element implicitly. 它将隐式地为每个元素调用.ToString

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

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