无法使用接口检索实例

时间:2012-01-26 22:08:18

标签: c# interface

目标:
检索List<>的所有实例来自界面GameFactory

问题:
我检索错误消息“接口不能包含字段”。

我应该怎么做才能从界面GameFactory中检索所有实例。有什么好主意吗?

// Fullmetalboy

public interface GameFactory<T>
{



}

public class NiceGameFactory : GameFactory<Player>
{

    public List<Obstacle> _my_G_Obstacle;
    public List<Action> _my_G_Action = new List<Action>();
    public Player _myPlayer;
    public void RetrieveObstacle()
    {



    }

    public NiceGameFactory()
    {

        _my_G_Obstacle = new List<Obstacle>();
        _my_G_Obstacle.Add(new Wizard());
        _my_G_Obstacle.Add(new Wall());
        _my_G_Obstacle.Add(new Elephant());
        _my_G_Obstacle.Add(new DPexam());

        _my_G_Action.Add(new BowAndShakeHandsAction());
        _my_G_Action.Add(new RunAndHideAction());
        _my_G_Action.Add(new BargainAndBuyAction());
        _my_G_Action.Add(new ChainsawAction());
        _my_G_Action.Add(new ClimbAction());
        _my_G_Action.Add(new CastAspellAction());
        _my_G_Action.Add(new SurrenderAction());

        _myPlayer = new OrdinaryPlayer();
    }


}

public class NastyGameFactory
{

    public List<Obstacle> _my_G_Obstacle;
    public List<Action> _my_G_Action;
    public Player _myPlayer;


    public NastyGameFactory()
    {
        _my_G_Obstacle.Add(new Dragon());
        _my_G_Obstacle.Add(new Bomb());
        _my_G_Obstacle.Add(new Samuray());
        _my_G_Obstacle.Add(new SumoWrestler());
        _my_G_Obstacle.Add(new Gangster());

        _my_G_Action.Add(new BowAndShakeHandsAction());
        _my_G_Action.Add(new RunAndHideAction());
        _my_G_Action.Add(new SwordAction());
        _my_G_Action.Add(new RifleAction());
        _my_G_Action.Add(new OfferFoodAction());
        _my_G_Action.Add(new ChainsawAction());
        _my_G_Action.Add(new CastAspellAction());
        _my_G_Action.Add(new BargainAndBuyAction());
        _my_G_Action.Add(new SurrenderAction());

        _myPlayer = new HeroPlayer();
    }


}

1 个答案:

答案 0 :(得分:1)

您可以通过界面上的属性将列表定义为可访问。

public interface GameFactory <T>
{

  public List<Obstacle> _my_G_Obstacle { get; set; }
  public List<Action> _my_G_Action { get; set; }

}

(无论如何,这都是比公共领域更好的做法。)