父类的C#属性继承

时间:2011-12-26 14:21:56

标签: c# list inheritance parent

我希望我的Player对象继承锦标赛中的筹码总和。我得到了(尝试除以零。)我认为这是因为它无法从父级Tournament对象继承Chips。为什么这不起作用?

public partial class Form1 : Form
{
    public Tournament Tournament { get; set; }

    public Form1()
    {
        InitializeComponent();

        Tournament = new Tournament();
        Tournament.PlayerList.Add(new Player("Hero", 5000));
        Tournament.PlayerList.Add(new Player("Villain1", 3000));
        Tournament.PlayerList.Add(new Player("Villain2", 4000));

        MessageBox.Show(Tournament.PlayerList[0].StackPercentage.ToString());
    }
}

public class Tournament
{
    public List<Player> PlayerList { get; set; }
    public int Chips
    {
        get
        {
            return PlayerList.Sum(S => S.Stack);
        }
    }

    public Tournament()
    {
        PlayerList = new List<Player>();
    }
}

public class Player : Tournament
{
    public string ID { get; set; }
    public int Stack { get; set; }
    public double StackPercentage
    {
        get
        {
            return Stack / Chips;
        }
    }

    public Player(string _ID, int _Stack)
    {
        ID = _ID;
        Stack = _Stack;
    }
}

3 个答案:

答案 0 :(得分:6)

你不应该从锦标赛派生玩家。通过这样做,你说每个玩家都是一个锦标赛。

尝试这样的事情:

public interface IPlayer {}

public abstract class Player : IPlayer 
{
  public ITournament CurrentTournament { get; set; }
}

public class PokerPlayer : Player 
{
  public PokerPlayer() {}
  public int CurrentChips { get; set; }

  public double StackPercentage 
  {
    get { return this.CurrentChips / (PokerTournament)this.CurrentTournament.StartingChips; }
  }
}

public interface ITournament {}

public abstract class Tournament : ITournament
{
  public List<Player> Players { get; set; }
  public int PlayerCount { get { return this.Players.Count; } }
}

public class PokerTournament : Tournament 
{
  private PokerTournament() {}
  public PokerTournament(int startingChips)
  { 
    if(startingChips < 1) throw new ArgumentException("Invalid starting amount.", "startingChips");
    this.StartingChips = startingChips; 
  }

  public int StartingChips { get; set; }
}

答案 1 :(得分:1)

继承看起来很好。

我认为你的问题是PlayerList可能是空的,或者Player中每个List的筹码都等于,因此sum也是

StackPercentage考虑Chips等于的可能性,如果它有意义或修复填充PlayerList的逻辑以便Chips 1}}不能

编辑:您已编辑过您的问题,但您编写的代码甚至无法编译......除非您澄清您的代码,否则我不确定您的问题是什么。

试试这个:

public Form1()
{
    ....
    Tournament tournament = new Tournament();
    tournament.PlayerList.Add(new Player("Hero", 5000));
    tournament.PlayerList.Add(new Player("Villain1", 3000));
    tournament.PlayerList.Add(new Player("Villain2", 4000));
    int chips = tournament.PlayerList[0].StackPercentage.ToString();
}

无论如何这不会奏效。有关详细信息,请参阅Haris Hasan's答案。基本上tournament(Tournament)PlayerList[0]不是同一个对象。请记住,每次创建Player实例时,都会创建一个新的Tournament基类实例及其相应的emtpy PlayerList

你根本不应该使用继承。 PlayerTournament根本没有继承关系。 Player不是Tournament。摆脱继承关系,只需将StackPercentage移动到Tournament类,并将其转换为以Player实例作为参数的方法:

  public double StackPercentage(Player player)
  {
       return player.Stack/this.Chips;
  }

答案 2 :(得分:1)

此代码中的问题是您假设Tournament.PlayerList[0]将指向与实例Tournament正在执行的相同的播放器列表,但情况并非如此。 PlayerList[0]的PlayerList集合为空。

 public double StackPercentage
    {
        get
        {
            //Chips here is 0 because the PlayerList of Player instance is empty
            return Stack / Chips;
        }
    }

Player类拥有自己的PlayerList