从派生类到基类

发布于 2024-12-10 19:11:31 字数 1894 浏览 0 评论 0原文

我正在尝试创建一个足球模拟程序。我有一个名为“team”的主类和4个名为“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");
    }
}

如您所见,我正在根据每个球员的技术属性计算他们的影响力。

我想要的是自动化这个过程。例如,我创建了一个团队..添加了玩家,并且我希望通过团队名称来调用所有玩家的影响力。我将给出球队名称和位置名称,这将为我提供该球队所选位置中球员的平均影响力。

我该怎么做?

预先感谢...

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

i am trying to create a football simulation program. i have a main class named "team" and 4 derived classes named "goalKeeper", "defender", "forward" and "midfielder".

i am creating players according to their position.
ex:

team fb = new team("fb");
forward alex = new forward(fb.tName, "alex", 73, 77, 77, 69, 70);

my team class:

  public class team
{
    public string tName;

    public team(string tName)
    {
        this.tName = tName;

    }
    public string teamInfo()
    {
        return tName;
    }
}

forward class:

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

as you can see i am calculating influence of each player according to their technical attributes.

what i want is automating the process. for example i've created a team.. added players, and i want all player's influence to be called via team name. i am going to give team name and position name and it is gonna give me average influence of players in that team's chosen position.

how can i do this?

thanks in advance...

note: my code might look stupid. i am a newbie :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦途 2024-12-17 19:11:31

前锋是一支球队吗?
完全不是……一个团队有一个前锋……

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

A forward IS A team?
Not at all... A team HAS A forward...

Dont use inheritance... use composition instead.

〆一缕阳光ご 2024-12-17 19:11:31

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

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

A player is not a team, this would give you an idea

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 }
  }
}
〗斷ホ乔殘χμё〖 2024-12-17 19:11:31

我建议重新考虑您的继承策略。
当一个类继承另一个类时,这意味着子类“是”基类。将此应用于您的模型意味着前锋“是一个”团队,这没有多大意义。事实上,一支球队“有”一名前锋。
对于您想要实现的目标,一个更准确的模型是将球员类别作为您的前锋类别、后卫类别等继承的基类别。然后,您的团队类可以包含玩家类的集合。

I would suggest rethinking your inheritence strategy.
When a class inherits another this implies that the child class 'is a' base class. Applying this to your model implies that a forward 'is a' team which doesn't make much sense. In reality a team 'has a' forward(s).
A more accurate model for what you are trying to achieve is to have a player class as your base class that your foward class, defender class etc inherits from. Your team class can then contains a collection of player classes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文