实例化许多派生类之一 - C#

时间:2017-10-08 05:11:22

标签: c# unity5

我有一个问题,我有一些派生自一个Abstract类的类。

public abstract class Bet
{
    public int Amount { get; set; }

    public abstract void LockUpChips();
}

我还有一个类附加到场景周围的各种GameObjects。它有一个名为BetType的枚举:

public class BetArea : MonoBehaviour
{
    public BetType Type;
    public int Number;
    public int NumberTwo;
...

一些派生类:

public class PassBet : Bet, IBet
{
    public PassBet(int amt)
    {
        Amount = amt;
    }

    public void Adjudicate()
    {
        int result = GameManager.Instance.DieResults[0] + GameManager.Instance.DieResults[1];
        if (GameManager.Instance.Point == 0)
        {
            switch (result)
            {
                case 2:
                case 3:
                case 12:
                    GameManager.Instance.LosingBets.Add(this);
                    break;
                case 7:
                case 11:
                    GameManager.Instance.PlayerMoney += GetPayout();
                    break;
            }
        }
        else
        {
            if(result == 7)
            {
                GameManager.Instance.LosingBets.Add(this);
            }
            else if (result == GameManager.Instance.Point)
            {
                GameManager.Instance.PlayerMoney += GetPayout();
            }
        }
    }

    public decimal GetPayout()
    {
        return Amount;
    }

    public override void LockUpChips()
    {
        GameManager.Instance.ClearBetArea(BetType.Pass);
    }
}

public class PlaceBet : Bet, IBet
{
    public int BetNumber { get; set; }

    public PlaceBet(int num, int amt)
    {
        Amount = amt;
        BetNumber = num;
    }

    public void Adjudicate()
    {
        int result = GameManager.Instance.DieResults[0] + GameManager.Instance.DieResults[1];
        if (GameManager.Instance.Point != 0)
        {
            if (result == 7)
            {
                GameManager.Instance.LosingBets.Add(this);
            }
            else if (result == BetNumber)
            {
                GameManager.Instance.PlayerMoney += GetPayout();
            }
        }
    }

    public decimal GetPayout()
    {
        decimal Multiplier = 0m;
        switch (BetNumber)
        {
            case 4:
            case 10:
                Multiplier = (9m / 5m);
                break;
            case 5:
            case 9:
                Multiplier = (7m / 5m);
                break;
            case 6:
            case 8:
                Multiplier = (7m / 6m);
                break;
        }
        return Amount * Multiplier;
    }
    public override void LockUpChips()
    {
        GameManager.Instance.ClearBetArea(BetType.Place);
    }
}

我发现自己陷入了糟糕的境地。后来BetArea类定义是:

public void Clicked()
{
    switch (Type)
    {
        case BetType.Pass:
            GameManager.Instance.Bets.Add(new PassBet(GameManager.Instance.BetDenomination));
            //do lots of other things
            break;
        case BetType.Place:
            GameManager.Instance.Bets.Add(new PlaceBet(Number, GameManager.Instance.BetDenomination));
            //do lots of other things slightly differently
            break;
...

GameManager.Instance.Bets是List(派生类继承的接口)。我想在BetArea类的另一个方法中对此进行参数化...我一直在尝试类似:

private void IncreaseOrCreateBet<T>() where T : Bet
    {
        GameManager.Instance.Bets.Add(new T()); // :(
    }

......哪个不行。我仍然掌握多态性,所以任何帮助都会受到赞赏。

0 个答案:

没有答案