【问题标题】:Cant Figure out this c# reference无法弄清楚这个 c# 参考
【发布时间】: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#


    【解决方案1】:

    当您进行此初始化时:

    Movie movie = new Movie();
    

    空的构造函数

    public Movie() : this("Jaws", GenreType.Action, 4) { } 
    

    调用具有多个参数的重载构造函数:

    public Movie(String title, GenreType type, int rating) { ... }
    

    当你删除这一行时:this("Jaws", GenreType.Action, 4) { },现在发生的事情是你只是调用了一个什么都不做的空构造函数。

    所以当你打电话时

    int ratingValue = movie.Rating;
    

    返回整数的默认值zero,因为您确实对其进行了任何设置。

    更新

    一个简单的if也许,如果我明白你的意思

    假设Rating 应该大于零。

    public override string ToString()
    {     
        if (Rating == 0)
        {
            return String.Format("{0}", Title);
        }
        else
        {
            return String.Format(" {0} Genre : {1},  Rating: {2:d} Stars. ", Title, GenType, Rating);
        }
    }
    

    【讨论】:

    • 感谢您的回答,我现在更了解发生了什么,但我该如何阻止类型:动作,评分:0 星被打印出来?
    • stop Genre : Action, Rating: 0 Stars from being printed out 是什么意思? 困惑
    【解决方案2】:

    这是因为enumint 总是初始化为默认值0。

    它不像string - 如果你不初始化它将等于null。如果您想模拟int 的这种行为,您可以随时尝试使用int? 类型。

    要了解有关此主题的更多详细信息,请查看Default Values Table (C#)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多