说我有一个课程如下
class FootballPlayer
{
public string Name { get; set; }
public string TeamName { get; set; }
public int CareerGoals { get; set; }
public int CareerAssists { get; set; }
public int CareerPasses { get; set; }
public int SeasonGoals { get; set; }
public int SeasonAssists { get; set; }
public int SeasonPasses { get; set; }
public int CurrentMatchGoals { get; set; }
public int CurrentMatchAssists { get; set; }
public int CurrentMatchPasses { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
但我想更好地组织它,所以我该怎么做呢.此刻我尝试过这样的事情 -
class CareerDetails
{
public int Goals;
public int Assists;
public int Passes;
}
class CurrentMatchDetails
{
public int Goals;
public int Assists;
public int Passes;
public bool IsCaptain;
}
class GeneralDetails
{
public string Name;
public string TeamName;
}
class SeasonDetails
{
public int Goals;
public int Assists;
public int Passes;
public int MatchesPlayed;
}
class FootballPlayer
{
public CurrentMatchDetails CurrentMatchDetails { get; set; }
public SeasonDetails SeasonDetails { get; set; }
public CareerDetails CareerDetails { get; set; }
public GeneralDetails GeneralDetails { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我不喜欢的一些事情
但我不确定这是否是最好的做法.我正在考虑在类FootballPlayer中创建一个嵌入式类.
我将在WPF应用程序中使用该类并INotifyPropertyChanged在我的FootballPlayer类上实现.通过使用上面的方法,我将不得不在所有类等上使用INPC CareerDetails.那么我应该做什么,或者我应该坚持我拥有的东西?
我可能有另一个名为'FootballTeam'的基类,它可以有一个名为CurrentMatchDetails的子类 - 它可能看起来像这样
class CurrentMatchDetails
{
public double TimeElapsed;
public string RefereeName;
}
Run Code Online (Sandbox Code Playgroud)
所以我应该能够访问像teamObject.CurrentMatchDetails.RefereeName或playerObject.CurrentMatchDetails.Goals这样的属性;
你应该创建一个StatDetails对象:
class FootballPlayer
{
public FootballPlay()
{
CareerStats = new StatDetails();
SeasonStats = new StatDetails();
CurrentStats = new StatDetails();
}
public string Name { get; set; }
public string TeamName { get; set; }
public StatDetails CareerStats { get; set; }
public StatDetails SeasonStats { get; set; }
public StatDetails CurrentStats { get; set; }
}
class StatDetails
{
public int Goals { get; set; }
public int Assists { get; set; }
public int Passes { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这样,您只需要INotifyPropertyChanged在两个类上实现.