如何使用Program.cs中子类的变量?我只能使用Player类中声明的那些。但是我能够“合并”子类参数与父类的变量
但是当我尝试使用这些变量时,它们会丢失:
如何从子类中调用变量?如果可能的话。
abstract class Player
{
public string Team { get; set; }
public string Name { get; set; }
public string surName { get; set; }
public DateTime birthDate { get; set; }
public int gamesCount { get; set; }
public Player()
{ }
public Player(string Team, string Name, string surName, DateTime birthDate, int gamesCount)
{
this.Team = Team;
this.Name = Name;
this.surName = surName;
this.birthDate = birthDate;
this.gamesCount = gamesCount;
}
}
class BasketballPlayers : Player
{
public int Rebounds { get; set; }
public int Assists { get; set; }
public int scorePoints { get; set; }
public BasketballPlayers()
{ }
public BasketballPlayers(string Team, int gamesCount, string Name, string surName, DateTime birthDate, int scorePoints, int Rebounds, int Assists)
: base(Team, Name, surName, birthDate, gamesCount)
{
this.scorePoints = scorePoints;
this.Rebounds = Rebounds;
this.Assists = Assists;
}
class FootballPlayers : Player
{
public int yellowCards { get; set; }
public int scorePoints { get; set; }
public FootballPlayers()
{ }
public FootballPlayers(string Team, int gamesCount, string Name, string surName, DateTime birthDate, int scorePoints, int yellowCards)
: base(Team, Name, surName, birthDate, gamesCount)
{
this.yellowCards = yellowCards;
this.scorePoints = scorePoints;
}
更新:PlayersContainer bPlayers = new PlayersContainer(100)
用于篮球运动员
PlayersContainer fPlayers = new PlayersContainer(100);
这是给足球运动员的。
static void ReadDataFromFiles(string basketBallFile, string footBallFile, string teamInfoFile, PlayersContainer bPlayers, PlayersContainer fPlayers)
{
ReadPlayerData(basketBallFile, bPlayers);
ReadPlayerData(footBallFile, fPlayers);
}
static void ReadPlayerData(string filePath, PlayersContainer Players)
{
using (StreamReader reader = new StreamReader(filePath))
{
string line = null;
string playerType = null;
line = reader.ReadLine();
if (line != null)
{
playerType = line;
}
int i = 0;
while (null != (line = reader.ReadLine()))
{
string[] values = line.Split(';');
string Team = values[0];
string surName = values[1];
string Name = values[2];
DateTime birthDate = DateTime.Parse(values[3]);
int gamesCount = int.Parse(values[4]);
int scorePoints = int.Parse(values[5]);
switch (filePath)
{
case "../../Krepsininkai.csv":
int Rebounds = int.Parse(values[6]);
int Assists = int.Parse(values[7]);
BasketballPlayers bPlayer = new BasketballPlayers(Team, gamesCount, Name, surName, birthDate, scorePoints, Rebounds, Assists);
Players.AddPlayer(bPlayer);
break;
case "../../Futbolininkai.csv":
int yellowCards = int.Parse(values[6]);
FootballPlayers fPlayer = new FootballPlayers(Team, gamesCount, Name, surName, birthDate, scorePoints, yellowCards);
Players.AddPlayer(fPlayer);
break;
}
}
}
}
当我调试时,我可以看到添加了那些排他性参数,我的意思是Rebounds, Assists, scorePoints
对于足球运动员,yellowCards, scorePoints
对于足球运动员。但是然后我尝试访问它们,为此我需要一种方法还是什么?我只能调用基类参数。
答案 0 :(得分:-2)
最好通过创建一个具有要调用的属性的Abstract Base Class来设计此需求。只是让派生类或子类覆盖属性即可。然后,当一个具有引用或通过与基础相关的接口的引用时,如果它的实例确实是子代,则调用/使用子代的属性。