[英]from derived class to base class
我正在尝试创建一个足球模拟程序。 我有一个名为“ team”的主类和四个名为“ goalKeeper”,“ defender”,“ forward”和“ midfielder”的派生类。
我根据他们的位置来创建球员。 例如:
team fb = new team("fb");
forward alex = new forward(fb.tName, "alex", 73, 77, 77, 69, 70);
我的班级:
public class team
{
public string tName;
public team(string tName)
{
this.tName = tName;
}
public string teamInfo()
{
return tName;
}
}
升等班:
class forward:team
{
//özellikler
public string pName;
public string pPosName;
public int finishing;
public int longShots;
public int composure;
public int offTheBall;
public int firstTouch;
public forward(string tName, string pName, int finishing, int longShots, int composure, int offTheBall, int firstTouch)
: base(tName)
{
this.pName = pName;
this.pPosName = "Forward";
this.finishing = finishing;
this.longShots = longShots;
this.composure = composure;
this.offTheBall = offTheBall;
this.firstTouch = firstTouch;
}
//etkiyi hesapla
public double influence
{
get
{
//calculations
return processed;
}
}
//futbolcunun genel bilgileri
public void playerInfo()
{
Console.WriteLine( "\n##############################\n" + pName + "-" + tName + "-" + pPosName + "\n" + "Finishing= " + finishing + "\n" + "Long Shots= " + longShots + "\n" + "Composure= " + composure + "\n" + "Off the ball= " + offTheBall + "\n" + "Frist Touch= " + firstTouch + "\n##############################\n");
}
}
如您所见,我正在根据每个球员的技术属性来计算他们的影响力。
我想要的是自动化过程。 例如,我已经创建了一个团队..增加了球员,我希望通过球队名称来调用所有球员的影响力。 我将提供球队名称和职位名称,这将使我了解该球队所选职位上球员的平均影响力。
我怎样才能做到这一点?
提前致谢...
注意:我的代码可能看起来很愚蠢。 我是新手:)
前锋是一支球队吗? 一点也不...一支球队有前锋...
不要使用继承...而是使用合成。
球员不是球队,这会给你一个想法
public class Team
{
private IList<Player> _players
...
}
public class Player
{
public string Name {get;set;}
public abstract Influence { get; }
}
public class Forward : Player
{
public override Influence
{
get { return //calculation }
}
}
我建议重新考虑您的继承策略。
当一个类继承另一个类时,这意味着子类“是”基类。 将其应用于您的模型意味着前锋“是”团队并没有多大意义。 实际上,一个团队“有一个”前锋。
对于您要实现的目标,更准确的模型是将玩家类作为您的前锋类,后卫类等继承的基类。 然后,您的团队班级可以包含一组玩家班级。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.