2011-10-18 189 views
0

我正在尝试创建一个足球模拟程序。我有一个名为“团队”的主类和4个名为“守门员”,“后卫”,“前锋”和“中场球员”的派生类。从派生类到基类

我根据他们的位置创建球员。 例如:

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"); 
    } 
} 

,你能看到我的根据自己的技术属性计算每个球员的影响力。

我想要的是自动化过程。例如,我创建了一支球队......增加了球员,我希望所有球员的影响力都通过球队名称进行调用。我打算给出球队名称和阵地名称,这会给我在球队所选位置上的球员的平均影响力。

我该怎么做?

在此先感谢...

注意:我的代码可能看起来很愚蠢。我是一个新手:)

+3

你应该只使用继承模型的是,有关系(和其他一些关系)一名球员是不是球队因此它的通常不是一个好主意从一个团队派生出一个球员 –

+1

你应该马上改变一些东西。守门员/后卫/前锋/中场球员不是**球队,而是球员。调用你的基类玩家,并在某个地方可以有一个由一组玩家对象组成的团队。 –

+0

只是一个想法 - 你不会考虑前锋,守门员,中场和后卫是*球员类型*。然后一支球队会成为球员的一个*集合? – StuartLC

回答

1

球员是不是一个团队,这将给你一个想法

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 } 
    } 
} 
+0

我正在尝试这个。谢谢 – dum

2

前锋是一个团队? 一点都不......一支球队有前锋...

不要使用继承...使用组合而不是。

0

我建议重新考虑你的继承策略。
当一个类继承另一个时,这意味着子类'是'基类。将这个应用到你的模型意味着一个前锋'是'一个没有多大意义的团队。事实上,一支球队'有'前锋。
一个更准确的模型,你想要实现的是让一个玩家类作为你的基类,你的前级,后卫类等继承。然后你的团队类可以包含一系列玩家类。