我有一个包含许多不同数据成员的数据类型 PlayerStats。我想为每个数据成员计算一个不同的分数(下面的案例查看 statistics.nrOfGoals)。
private double getScore()
{
double strength = 0;
foreach (PlayerStats statistics in this.statistics)
{
double dateDiff = Math.Abs(nowDate.Subtract(statistics.date).Days / (365.25 / 12));
dateDiff = Math.Pow(dateDiff, Form1.historyFactor);
strength += (statistics.nrOfGoals * ValueTable.PointsPerGoals ) / dateDiff;
}
return strength;
}
如何使此函数通用并接受要查看的数据成员,而不是创建许多外观相似的函数?
就像是
private double getScore(Type type, Type type2)
{
double strength = 0;
foreach (PlayerStats statistics in this.statistics)
{
double dateDiff = Math.Abs(nowDate.Subtract(statistics.date).Days / (365.25 / 12));
dateDiff = Math.Pow(dateDiff, Form1.historyFactor);
strength += (statistics.type * ValueTable.type2) / dateDiff;
}
return strength;
}