因此,我想设计这样的团队/球员关系:每个球员都属于一个团队,但是由于我想通过界面进行练习,所以我创建了ITeam和IAthlete,然后创建了BasketballTeam和BasketballPlayer。
然后我写了这段代码:

public interface IAthlete
{
     string GetName();
     string GetSport();
}

public interface ITeam
{
     void AddPlayer(IAthlete player);
     IAthlete[] GetAthletes();
     string GetName();
     int GetNumberOfPlayers();
}

public class BasketballPlayer:IAthlete
{
    private string name;

    public string GetName()
    {
        return this.name;
    }

    public string GetSport()
    {
        return "Basketball";
    }

    public BasketballPlayer(string name)
    {
        this.name = name;
    }

    public void Run(int distance)
    {
        Console.WriteLine(this.name + " just ran " + distance.ToString() + " meters.");
    }

    public bool Shoot()
    {
        Console.WriteLine("Successful shot for " + this.name);
        return true;
    }
}

public class BasketballTeam: ITeam
{
    BasketballPlayer[] players;
    int numberOfPlayers;
    private string name;

    public void AddPlayer(BasketballPlayer player)
    {
        this.players[this.numberOfPlayers] = player;
        this.numberOfPlayers++;
    }

    public IAthlete[] GetAthletes()
    {
        return this.players;
    }

    public string GetName()
    {
        return this.name;
    }

    public int GetNumberOfPlayers()
    {
        return this.numberOfPlayers;
    }

    public BasketballTeam(string name)
    {
        this.numberOfPlayers = 0;
        this.name = name;
        this.players = new BasketballPlayer[10];
    }
}

class Program
{
    static void Main(string[] args)
    {
        BasketballTeam bt = new BasketballTeam("MyTeam");
        BasketballPlayer bp = new BasketballPlayer("Bob");

        bt.AddPlayer(bp);

        foreach (BasketballPlayer player in bt.GetAthletes())
        {
            Console.WriteLine(player.GetName());
        }

        foreach (IAthlete a in bt.GetAthletes())
        {
            Console.WriteLine(a.GetName());
        }
    }
}


但这不会编译,因为我正在使用以下代码:

public void AddPlayer(BasketballPlayer player)


在BasketballPlayer中而不是这个

public void AddPlayer(IAthlete player)


我认为这应该起作用,因为BasketballPlayer是IAthlete。
如果我将其更改为IAthlete,则可以进行如下这样的另一个类:

public class HockeyPlayer : IAthlete
{
    private string name;

    public string GetName()
    {
        return this.name;
    }

    public string GetSport()
    {
        return "Hockey";
    }

    public HockeyPlayer(string name)
    {
        this.name = name;
    }

    public void Run(int distance)
    {
        Console.WriteLine(this.name + " just ran " + distance.ToString() + " meters.");
    }
}


然后在我的主体中执行此操作:

 HockeyPlayer hp = new HockeyPlayer("Henry");
 bt.AddPlayer(hp);


这在逻辑上是错误的,因为我正在将HockeyPlayer添加到BasketballTeam。是否应该是这样,我应该小心不要这样做?我究竟做错了什么?如何使用类图显示此内容?这会导致耦合松散吗?

最佳答案

这是您对代码的一些想法。首先,在C#中,可以使用属性代替Get和Set方法。

public interface IAthlete
{
    string Name { get; }
    string Sport { get; }
}


使用自动属性,您可以要求编译器为属性生成后台存储。还可以考虑创建基类Player,该类将保留Name和Sport属性的实现。

public class Player : IAthlete
{
    public Player(string name, string sport)
    {
        Name = name;
        Sport = sport;
    }

    public string Name { get; private set; }
    public string Sport { get; private set; }
}


现在,当实现某个播放器时,您只需将值传递给基类构造函数即可。而且您的自定义播放器将仅保留特定于它们的功能(没有代码重复)。另外,建议使用字符串格式,而不是连接字符串:

public class BasketballPlayer : Player
{
    public BasketballPlayer(string name)
        : base(name, "Basketball")
    {
    }

    public void Run(int distance)
    {
        Console.WriteLine("{0} just ran {1} meters.", Name, distance);
    }

    public bool Shoot()
    {
        Console.WriteLine("Successful shot for " + Name);
        return true;
    }
}


现在关于团队。如果您不想在BasketballTeam中加入Football Player,那么您应该创建参数化的团队。还可以考虑使用IEnumerable:

public interface ITeam<TPlayer>
    where TPlayer : IAthlete
{
    void AddPlayer(TPlayer player);
    IEnumerable<TPlayer> Players { get; }
    string Name { get; }
    int NumberOfPlayers { get; }
}


同样,对于通用功能,您可以创建基类。请记住,在添加新玩家之前,您应该检查团队中有多少玩家。

public class Team<TPlayer> : ITeam<TPlayer>
    where TPlayer : IAthlete
{
    private readonly List<TPlayer> _players = new List<TPlayer>();

    public Team(string name, int teamSize)
    {
        Name = name;
        TeamSize = teamSize;
    }

    public void AddPlayer(TPlayer player)
    {
        if (_players.Count == TeamSize)
            throw new Exception("Players number exceeded");

        _players.Add(player);
    }

    public string Name { get; private set; }
    public int TeamSize { get; private set; }

    public IEnumerable<TPlayer> Players
    {
        get { return _players; }
    }

    public int NumberOfPlayers
    {
        get { return _players.Count; }
    }
}


定制团队的实施变得非常容易。您只需说出它将拥有的球员类型,然后传递给基本团队实施团队名称和团队规模。

public class BasketballTeam : Team<BasketballPlayer>
{
    public BasketballTeam(string name)
        : base(name, 10)
    {
    }
}


现在,您的程序就像一个魅力一样工作:

class Program
{
    static void Main(string[] args)
    {
        BasketballTeam bt = new BasketballTeam("MyTeam");
        BasketballPlayer bp = new BasketballPlayer("Bob");

        bt.AddPlayer(bp);

        foreach (BasketballPlayer player in bt.Players)
        {
            Console.WriteLine(player.Name);
        }

        foreach (IAthlete a in bt.Players)
        {
            Console.WriteLine(a.Name);
        }
    }
}

08-08 06:17