public class Player
{
private string _name;
public string Name
{
get
{
return _name;
}
set
{
_name = value
}
}
}
public class Player
{
public string Name { get; set; }
}
public class Player
{
private string _name;
public string Name
{
get
{
return _name;
}
set
{
_name = value
}
}
}
public class Player
{
public string Name { get; set; }
}
_Name
.public class Player
{
public string Name { get; set; }
public List<int> Turns { get; } = new List<int>();
}
Control.Controls
, ComboBox.Items
, DataTable.Rows
, etc, etc.public class Player
{
public string Name { get; set; }
public bool PlayerIsComputer { get; set; }
public int RoundsPlayed { get; set; }
public List<int> Turns { get; } = new List<int>();
public Player(string name, bool playerIsComputer, int roundsPlayed)
{
Name = name;
PlayerIsComputer = playerIsComputer;
RoundsPlayed = roundsPlayed;
}
}
roundsPlayed
parameter either. Won't that always be zero for a new Player
? If so, there's no point using a parameter and you can just set the property explicitly to zero.Turns
then you could do it like this:public class Player
{
public string Name { get; set; }
public bool PlayerIsComputer { get; set; }
public int RoundsPlayed { get; set; }
public List<int> Turns { get; } = new List<int>();
public Player(string name, bool playerIsComputer, int roundsPlayed)
{
Name = name;
PlayerIsComputer = playerIsComputer;
RoundsPlayed = roundsPlayed;
}
public Player(string name, bool playerIsComputer, int roundsPlayed, IEnumerable<int> turns)
: this(name, playerIsComputer, roundsPlayed)
{
Turns.AddRange(turns);
}
}
turns
values to the existing collection. You could then create a new Player
like this:players.Add(new Player(ShuffleName(), true, 0));
players.Add(new Player(ShuffleName(), true, 0, new [] {1, 2, 3}));
turns
parameter is type IEnumerable<int>
, you can pass any type that implements that interface, which includes a 列表<int>
or an int
array or many other less common options.I was just suggesting that there was no need for a parameter in the constructor, because that property would be initialised to zero every time. It's not such a bad idea to have a dedicated property for that but you should have a single source of truth. As it stands, it would be possible for the回合应该为每个回合存储玩家点数。所以是的,我想我可以摆脱roundsPlayed字段,并通过循环转弯列表来获得计数。
Turns
collection to have a number of items that does not match the RoundsPlayed
property. A better option would be to implement RoundsPlayed
like this:public int RoundsPlayed
{
get
{
return Turns.Count;
}
}
I guess it depends exactly what you consider to be the fundamentals.我目前正在相当基本的水平上研究C#,现在我们已经达到了OOP。从初学者的角度了解基础知识。您使用IEnumerable的解决方案是否是最好的基础知识? IEnumerables至少到目前为止还没有涉及到。
IEnumerable<T>
is an interface and implementing interfaces is an important part of OOP, although probably a level or two up from basic. Essentially, any IEnumerable
is a list that you can enumerate using a foreach
loop and an IEnumerable<T>
is a strongly-type version of that. In this case, the idea is that you can pass any enumerable list as an argument. You could declare that parameter as int[]
or 列表<int>
but then you are limited to passing in only one type of object when the actual functionality of the constructor doesn't require that type. If you want to stick with types that you've already covered though, by all means use one of those types.You might define a
Round
class that had a property each for the three attempts and then a property for the sum of those scores. Your Player
class could then have a Rounds
property that was a 列表<Round>
. You might even have a TotalScore
property that was implemented as a sum of the scores for all rounds.我同意拥有一个带有三个属性的回合类来保存每个回合的值。 @jmcilhinney 在读我的思想,但在思考方面比我领先...这很接近 @jmcilhinney 毕竟是在建议,希望它没问题。您可以在早上04:30向男人期望什么类型的伪代码You might define aRound
class that had a property each for the three attempts and then a property for the sum of those scores. YourPlayer
class could then have aRounds
property that was a列表<Round>
. You might even have aTotalScore
property that was implemented as a sum of the scores for all rounds.
public class Game
{
public Player AddPlayer(Player player)
{
return player;
}
public Player RemovePlayer(Player player)
{
return player;
}
public Player CreateAIPlayer(Player player)
{
return player;
}
public void BeginGame()
{
}
}
public class Player
{
public Player(int roundsPlayed, string playerName, double playerScore, RoundScores roundScores, bool playerIsAI)
{
RoundsPlayed = roundsPlayed;
PlayerName = playerName;
PlayerScore = playerScore;
RoundScores = roundScores;
PlayerIsAI = playerIsAI;
}
public int RoundsPlayed { get; set; }
public string PlayerName { get; set; }
public double PlayerScore { get; set; }
public RoundScores RoundScores { get; set; }
public bool PlayerIsAI { get; set; }
}
public class Score
{
public Score(double totalScore)
{
TotalScore = totalScore;
}
public double GetTotalScore(RoundScores roundScores)
{
TotalScore = roundScores.Round1Score + roundScores.Round2Score + roundScores.Round3Score;
return TotalScore;
}
public List<Score> ScoresList = new List<Score>();
public double TotalScore { get; set; }
}
public class RoundScores
{
public double Round1Score { get; set; }
public double Round2Score { get; set; }
public double Round3Score { get; set; }
}
该GetTotalScore并不真正属于那里。每当使用用于存储数据的类时,最好仅出于该目的使用该类。相反,请将您拥有的所有方法转储到另一个单独的类中,甚至转储到某种类型的静态助手中以保存您的方法。它不仅使代码更整洁;因为它也有助于保持班级结构的单一责任规则。因此,我在其中鞭打错误。C#:public class Score { public Score(double totalScore) { TotalScore = totalScore; } public double GetTotalScore(RoundScores roundScores) { TotalScore = roundScores.Round1Score + roundScores.Round2Score + roundScores.Round3Score; return TotalScore; } public List<Score> ScoresList = new List<Score>(); public double TotalScore { get; set; } }
using System.Collections.Generic;
namespace ConsoleApp1
{
public class Program
{
public static void Main()
{
var p = new Player();
var r = new Round();
r.FirstAttempt = 16;
r.SecondAttempt = 5;
r.ThirdAttempt = 20;
p.RoundsPlayed.Add(r);
}
}
public class Player
{
public List<Round> RoundsPlayed { get; } = new List<Round>();
}
public class Round
{
public int FirstAttempt { get; set; }
public int SecondAttempt { get; set; }
public int ThirdAttempt { get; set; }
}
}