重写ToString方法时调用Freight.ToString报错,寻求解决方法
Hey there! Let's work through the issues in your code that are causing that error, plus a couple of other bugs you might run into later.
First, the immediate error: Freight.ToString() is invalid
The error pops up because you're trying to call the instance method ToString() directly on the Freight class itself, instead of on an actual instance of Freight.
Looking at your ToString() method in Truck:
sb.Append(string.Join(", ", freights)).Append(Freight.ToString());
The string.Join(", ", freights) part already does exactly what you want: it loops through every Freight in the list and calls each instance's ToString() method automatically. That extra .Append(Freight.ToString()) is not only unnecessary but also breaks the code. Just remove that part entirely.
Second, your freights list is never initialized properly
In your Truck constructor, you're creating a local List<Freight> instead of assigning it to the class's private freights field:
List<Freight> freights = new List<Freight>();
This means the class-level freights field stays null, and you'll hit a NullReferenceException when trying to access freights.Count. Fix this by dropping the type declaration so you're assigning to the class field:
freights = new List<Freight>(); // Or use this.freights explicitly to make it clear: // this.freights = new List<Freight>();
Third, your AddFreight method is empty
Right now, calling AddFreight doesn't actually add anything to your list. You need to implement the logic to add freight:
public void AddFreight(Freight freight) { freights.Add(freight); }
Here's the full corrected Truck class:
using System; using System.Collections.Generic; using System.Text; namespace TruckCompany // Fixed typo: was 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; // Initialize the class-level freights list, not a local one 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 { // string.Join automatically calls each Freight's ToString() sb.Append(string.Join(", ", freights)); } return sb.ToString(); } public IReadOnlyCollection<Freight> Freights { get => freights.AsReadOnly(); } public void AddFreight(Freight freight) { // Actually add the freight to the list freights.Add(freight); } } }
A quick note about your Freight class
Just make sure your Freight class has a properly overridden ToString() method (like you mentioned) so that string.Join works as expected. For example:
class Freight { public string Description { get; set; } public double Weight { get; set; } public override string ToString() { return $"{Description} ({Weight}kg)"; } }
That should fix all your current issues and get your ToString() method working correctly!
内容的提问来源于stack exchange,提问作者Victor Nikolov

