简体   繁体   中英

from derived class to base class

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 :)

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

Dont use inheritance... use composition instead.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM