【发布时间】:2014-11-13 13:29:11
【问题描述】:
我在默认构造函数中使用了“this”关键字,下面是电影类中的代码
namespace Movie_List
{ enum GenreType { Action, War, Drama, Thriller };
class Movie
{
//Data Members
private String _title;
private int _rating;
private GenreType _type;
//Properties
public GenreType GenType
{
get { return _type; }
set { _type = value; }
}
public String Title
{
get { return _title; }
set { _title = value; }
}
public int Rating
{
get { return _rating; }
set { _rating = value; }
}
public Movie()
: this("Jaws", GenreType.Action, 4) { }
public Movie(String title, GenreType type, int rating ) //working ctor
{
Title = title;
GenType = type;
Rating = rating;
}
public override string ToString()
{
return String.Format(" {0} Genre : {1}, Rating: {2:d} Stars. ", Title, GenType, Rating);
}
}
我想从文本文件中读取,所以我在 MainWindow.xaml.cs 中使用了这段代码
private void btnLoad_Click(object sender, RoutedEventArgs e)
{
string lineIn = "";
string[] filmarray;
using (StreamReader file = new StreamReader("filmlist.txt"))
{
while ((lineIn = file.ReadLine()) != null)
{
filmarray = lineIn.Split(new char[] { ',' });
moviecollection.Add(new Movie()
{
Title = filmarray[0],
GenType = (GenreType)Enum.Parse(typeof(GenreType), filmarray[1]),
Rating = Convert.ToInt32(filmarray[2]),
});
lstFilms.ItemsSource = moviecollection;
}
}
}
我现在不需要这段代码
: this("Jaws", GenreType.Action, 4)
但是当我删除它时,流派动作和评分 0 星仍然会打印出来。
有人知道为什么会发生这种情况吗?
【问题讨论】:
标签: c#