我正在尝试制作一个关于电影票预订系统的控制台应用程序。当我尝试将已存在的节目列表添加到 Movie 类中的 Shows 属性中时,总是说 Shows 为空或结果如下: 显示日期:System.Collections.Generic.List`1[ASM_TicketBooking.Show]
这是 Movie.cs 类
internal class Movie
{
private string title;
private string description;
private int durationMins;
private string language;
private DateTime releaseDate;
private string genre;
private List<Show> shows;
public string Title
{
get { return title; }
set { title = value; }
}
public string Description
{
get { return description; }
set { description = value; }
}
public int DurationMins
{
get { return durationMins; }
set { durationMins = value; }
}
public string Language
{
get { return language; }
set { language = value; }
}
public DateTime ReleaseDate
{
get { return releaseDate; }
set { releaseDate = value; }
}
public string Genre
{
get { return genre; }
set { genre = value; }
}
public List<Show> Shows
{
get { return shows; }
set { shows = value; }
}
public Movie()
{
shows = new List<Show>(_shows);
}
public Movie(string title, string description, int durationMins, string language, DateTime releaseDate, string genre, List<Show> shows)
{
Title = title;
Description = description;
DurationMins = durationMins;
Language = language;
ReleaseDate = releaseDate;
Genre = genre;
Shows = shows;
}
这是我将 Show 方法添加到 Shows 列表中的项目
public List<Show> addShow()
{
Show show1 = new Show(DateTime.ParseExact("2024-12-11 05:00:00", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture),
DateTime.ParseExact("2024-12-11 06:00:00", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture));
Show show2 = new Show(DateTime.ParseExact("2024-12-12 07:00:00", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture),
DateTime.ParseExact("2024-12-12 08:00:00", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture));
Show show3 = new Show(DateTime.ParseExact("2024-12-13 09:00:00", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture),
DateTime.ParseExact("2024-12-13 10:00:00", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture));
Show show4 = new Show(DateTime.ParseExact("2024-12-14 11:00:00", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture),
DateTime.ParseExact("2024-12-14 12:00:00", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture));
Show show5 = new Show(DateTime.ParseExact("2024-12-15 17:00:00", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture),
DateTime.ParseExact("2024-12-15 18:00:00", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture));
Shows.Add(show1);
Shows.Add(show2);
Shows.Add(show3);
Shows.Add(show4);
Shows.Add(show5);
Console.WriteLine("Add Shows successfully!");
return Shows;
}
我将电影添加到电影列表的方法
public List<Movie> addMovie()
{
Movie movie1 = new Movie("Romeo and Juliet: Afterlife", "Meadival Tragedy Romance",
300, "English", DateTime.ParseExact("2024-12-11 00:00:00", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture), "Romance", movie.Shows);
movies.Add(movie1);
Console.WriteLine("Add movies successfully!");
return movies;
}
以及我将结果输出到控制台的方法
public void getMovies()
{
foreach (Movie movie in movies)
{
Console.WriteLine(
$"Movie Title: {movie.Title}\n" +
$"Description: {movie.Description}\n" +
$"Duration: {movie.DurationMins}\n" +
$"Language: {movie.Language}\n" +
$"Genre: {movie.Genre}\n" +
$"Release Date: {movie.ReleaseDate}\n" +
$"Show Date: {movie.Shows}"
);
}
}
结果总是这样的:结果
这是默认的“ToString()”行为。基本上它向您显示通用列表对象的类型。如果您想要详细信息,您需要遍历列表并单独编写每个项目。
这是一个控制台应用程序示例:
var genericList = new List<string> { "one", "two", "three" };
Console.WriteLine(genericList.ToString());
Console.WriteLine(genericList.GetType().ToString());
Console.WriteLine();
foreach (var genericItem in genericList)
{
Console.WriteLine(genericItem);
}
输出:
System.Collections.Generic.List`1[System.String]
System.Collections.Generic.List`1[System.String]
one
two
three