覆盖返回 System.Collections.Generic.List`1[Problem_2.Movie] 的 ToString()
Override ToString() returning System.Collections.Generic.List`1[Problem_2.Movie]
所以我的重写 ToString() 方法有问题。它一直在我的文本框中输出 System.Collections.Generic.List`1[Problem_2.Movie] 而不是我需要的信息。
我的电影中覆盖了 ToString() 方法 Class。
class Movie
{
private string movieName { get; set; }
private int number { get; set; }
private string releaseDate { get; set; }
private string location { get; set; }
private string genre { get; set; }
private int rating { get; set; }
private int duration { get; set; }
private double price { get; set; }
public List<Movie> movList = new List<Movie>();
public Movie(string movieName,int number,string releaseDate,string location, string genre, int rating, int duration, double price)
{
this.movieName = movieName;
this.number = number;
this.releaseDate = releaseDate;
this.location = location;
this.genre = genre;
this.rating = rating;
this.duration = duration;
this.price = price;
}
public Movie()
{
//blank constructor
}
public override string ToString()
{
String movies ="";
for(int i = 0; i < movList.Capacity; i++)
{
movies += movList[i].movieName + "-" + movList[i].number + "-" + movList[i].releaseDate + "-"
+ movList[i].genre + "-" + movList[i].duration + ": $" + movList[i].price + "\n";
}
return movies;
}
我在单击按钮时调用它,然后应该在文本框中显示信息。
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = mov.movList.ToString();
}
您已经覆盖了 class Movie
的 ToString
方法。所以你必须在 Movie
的实例上调用它,而不是在 public 字段 movList
.
上调用它
textBox1.Text = mov.ToString();
ToString
方法实现注意事项:
您应该将 Capacity
属性 替换为 Count
属性:
for(int i = 0; i < movList.Count; i++)
Capacity
属性 根据 documentation:
Gets or sets the total number of elements the internal data structure
can hold without resizing.
而 Count
属性 (List.Count)
Gets the number of elements contained in the list
就是你想要的。
当您必须连接许多字符串时,您应该避免使用字符串连接。在后一种情况下,您应该更喜欢创建 StringBuilder
class 的实例并附加要连接的字符串。最后,通过在您创建的 StringBuilder
实例上调用 ToString
,您将获得想要连接的字符串的连接。
关于电影的注释class设计(正如你的问题的评论员已经指出的那样)
- 尝试使用常见的 C# 命名约定(Names of Type Members)
- 您公开了一个名为 movList 的 public 字段,它是
Movie
class 对象的列表,并且该字段定义在 Movie
class等级。这个选择有点迷惑。如果这个 class 代表一个 MovieStore,那么在 MovieStore 中有一个包含电影的列表是很合理的。电影列表在电影实例上代表什么?
- 最后但同样重要的是,class 代表电影有点奇怪,我们无法读取它的发布日期或持续时间等。您定义的属性都是私有的!
另一种设计可能是下面的。显然这里遗漏了很多东西,因为我是根据 假设 创建它的,我是通过阅读你的 class.
来做的
// Define an enum for Genre and populate with as many Genres you want.
enum Genre { Action, Adventure, Animation, Biography, Comedy, Crime }
// Define an enum for Rating and populate it accordingly
enum Rating { GeneralAudiences, ParentalGuidanceSuggested, ParentsStronglyCautioned, Restricted, AdultsOnly }
class Movie
{
public int Id { get; }
public string Name { get; }
public DateTime ReleaseDate { get; }
public string Location { get; }
public Genre Genre { get; }
public Rating Rating { get; }
// Prefer decimal for money. It's more accurate than double.
public decimal Price { get; }
public Movie(int id, string name, DateTime releaseDate, string location, Genre genre, Rating rating, Decimal price)
{
Id = id;
// It's meaningless we talk about for a Movie without a name.
Name = name ?? throw new ArgumentNullException(nameof(name));
ReleaseDate = releaseDate;
Location = location;
Genre = genre;
Rating = rating;
Price = price;
}
public override string ToString()
{
return $"Id: {Id}, Name: {Name}, ReleaseDate: {ReleaseDate.ToString("MM/dd/yyyy")}, Location: {Location}, Genre: {Genre}, Rating: {Rating}";
}
}
您可以在这里发现一些关于上述 class 的事情。
- 我们用枚举替换了 Genre 和 Rating 属性。这样做将使我们的代码更具可读性,并且我们将 指导 我们 class 的用户如何制作电影。考虑某人通过动作、其他动作、另一个动作等……作为电影的流派。这在我们的初始设计中是可行的。现在它只能从我们的枚举中选择一个值,例如
Genre.Action
。
- 以字符串形式询问发布日期并不是最好的方法。很可能有人会犯错误。想一想我们有两个呼叫者的情况,其中 none 他们在打字时犯了错误,但他们使用不同的格式,"dd/MM/yyyy" 和 "MM/dd/yyyy"!你会如何根据上映日期比较两部电影。通过将发布日期设为
DateTime
,您可以避免所有这些 糟糕的 情况。
- 我们将
double
替换为 decimal
,因为它更准确。
- 我们删除了默认构造函数。这样做,我们向 class 的用户展示了一种创建有效电影对象的方法。如果我们还包含了默认构造函数,那么就可以创建类似
var movie = new Movie { Location = "uknown"}
的电影,没有名称、发布日期等的电影!
所以我的重写 ToString() 方法有问题。它一直在我的文本框中输出 System.Collections.Generic.List`1[Problem_2.Movie] 而不是我需要的信息。
我的电影中覆盖了 ToString() 方法 Class。
class Movie
{
private string movieName { get; set; }
private int number { get; set; }
private string releaseDate { get; set; }
private string location { get; set; }
private string genre { get; set; }
private int rating { get; set; }
private int duration { get; set; }
private double price { get; set; }
public List<Movie> movList = new List<Movie>();
public Movie(string movieName,int number,string releaseDate,string location, string genre, int rating, int duration, double price)
{
this.movieName = movieName;
this.number = number;
this.releaseDate = releaseDate;
this.location = location;
this.genre = genre;
this.rating = rating;
this.duration = duration;
this.price = price;
}
public Movie()
{
//blank constructor
}
public override string ToString()
{
String movies ="";
for(int i = 0; i < movList.Capacity; i++)
{
movies += movList[i].movieName + "-" + movList[i].number + "-" + movList[i].releaseDate + "-"
+ movList[i].genre + "-" + movList[i].duration + ": $" + movList[i].price + "\n";
}
return movies;
}
我在单击按钮时调用它,然后应该在文本框中显示信息。
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = mov.movList.ToString();
}
您已经覆盖了 class Movie
的 ToString
方法。所以你必须在 Movie
的实例上调用它,而不是在 public 字段 movList
.
textBox1.Text = mov.ToString();
ToString
方法实现注意事项:
您应该将 Capacity
属性 替换为 Count
属性:
for(int i = 0; i < movList.Count; i++)
Capacity
属性 根据 documentation:
Gets or sets the total number of elements the internal data structure can hold without resizing.
而 Count
属性 (List.Count)
Gets the number of elements contained in the list
就是你想要的。
当您必须连接许多字符串时,您应该避免使用字符串连接。在后一种情况下,您应该更喜欢创建 StringBuilder
class 的实例并附加要连接的字符串。最后,通过在您创建的 StringBuilder
实例上调用 ToString
,您将获得想要连接的字符串的连接。
关于电影的注释class设计(正如你的问题的评论员已经指出的那样)
- 尝试使用常见的 C# 命名约定(Names of Type Members)
- 您公开了一个名为 movList 的 public 字段,它是
Movie
class 对象的列表,并且该字段定义在Movie
class等级。这个选择有点迷惑。如果这个 class 代表一个 MovieStore,那么在 MovieStore 中有一个包含电影的列表是很合理的。电影列表在电影实例上代表什么? - 最后但同样重要的是,class 代表电影有点奇怪,我们无法读取它的发布日期或持续时间等。您定义的属性都是私有的!
另一种设计可能是下面的。显然这里遗漏了很多东西,因为我是根据 假设 创建它的,我是通过阅读你的 class.
来做的// Define an enum for Genre and populate with as many Genres you want.
enum Genre { Action, Adventure, Animation, Biography, Comedy, Crime }
// Define an enum for Rating and populate it accordingly
enum Rating { GeneralAudiences, ParentalGuidanceSuggested, ParentsStronglyCautioned, Restricted, AdultsOnly }
class Movie
{
public int Id { get; }
public string Name { get; }
public DateTime ReleaseDate { get; }
public string Location { get; }
public Genre Genre { get; }
public Rating Rating { get; }
// Prefer decimal for money. It's more accurate than double.
public decimal Price { get; }
public Movie(int id, string name, DateTime releaseDate, string location, Genre genre, Rating rating, Decimal price)
{
Id = id;
// It's meaningless we talk about for a Movie without a name.
Name = name ?? throw new ArgumentNullException(nameof(name));
ReleaseDate = releaseDate;
Location = location;
Genre = genre;
Rating = rating;
Price = price;
}
public override string ToString()
{
return $"Id: {Id}, Name: {Name}, ReleaseDate: {ReleaseDate.ToString("MM/dd/yyyy")}, Location: {Location}, Genre: {Genre}, Rating: {Rating}";
}
}
您可以在这里发现一些关于上述 class 的事情。
- 我们用枚举替换了 Genre 和 Rating 属性。这样做将使我们的代码更具可读性,并且我们将 指导 我们 class 的用户如何制作电影。考虑某人通过动作、其他动作、另一个动作等……作为电影的流派。这在我们的初始设计中是可行的。现在它只能从我们的枚举中选择一个值,例如
Genre.Action
。 - 以字符串形式询问发布日期并不是最好的方法。很可能有人会犯错误。想一想我们有两个呼叫者的情况,其中 none 他们在打字时犯了错误,但他们使用不同的格式,"dd/MM/yyyy" 和 "MM/dd/yyyy"!你会如何根据上映日期比较两部电影。通过将发布日期设为
DateTime
,您可以避免所有这些 糟糕的 情况。 - 我们将
double
替换为decimal
,因为它更准确。 - 我们删除了默认构造函数。这样做,我们向 class 的用户展示了一种创建有效电影对象的方法。如果我们还包含了默认构造函数,那么就可以创建类似
var movie = new Movie { Location = "uknown"}
的电影,没有名称、发布日期等的电影!