简体   繁体   English

C# - 确定对象应该在实例化时使用哪个类?

[英]C# - Determine which class an object should use on instantiation?

I have 2 classes: Entity and Control.我有 2 个类:实体和控制。

Entity:实体:

public class Entity
{

   public float Rotate {get; private set;}

   readonly Control m_Control

   public Entity(float rot)
   {
     Rotate = rot;
     m_control = new Control();
   }

    public void Update(float time)
    {

            switch (m_control.Rotating())
            {
                case Control.Rotator.Right:
                    Rotate += time * 1.5f;
                    break;
                case Control.Rotator.Left:
                    Rotate -= time * 1.5f;
                    break;
                case Control.Rotator.Still:
                    break;
                default:
                    break;          
            }
     }



}

Control:控制:

public class Control
{
        private Random rnd = new Random();
        private int _randomTurn;

        public enum Rotator
        {
            Still,
            Right,
            Left
        }

        public Control()
        {
            TimerSetup(); // Initialize timer for Entity
        }

 public Rotator Rotating()
        {

                switch(_randomTurn)
                {
                    case 1:
                        return Rotator.Right;
                    case 2:
                        return Rotator.Left;
                    default:
                        return Rotator.Still;
                }

            }

 private void TimerSetup()
 {
            DispatcherTimer dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler(GameTickTimer_Tick);
            dispatcherTimer.Interval = new TimeSpan(0, 0, 2);
            dispatcherTimer.Start();
 }


 private void GameTickTimer_Tick(object sender, EventArgs e)
 {
     RandomTurn();
 }


 private void RandomTurn() 
 {
    _randomTurn = rnd.Next(1, 4);
 }


}

Basically I want to make 'Control' class a base class and create two subclasses: PlayerControl and AIControl.基本上我想让“Control”类成为基类并创建两个子类:PlayerControl 和 AIControl。 Currently both Player control inputs and AI control inputs are handled in the one Control class.目前,玩家控制输入和 AI 控制输入都在一个 Control 类中处理。

My dilemma is, in the Entity class, how do I determine what Control class the Entity will use?我的困境是,在 Entity 类中,如何确定 Entity 将使用哪个 Control 类?

The Entity class currently assigns the Control class like so: Entity 类当前分配 Control 类,如下所示:

readonly Control m_Control

public Entity(float rot)
   {
     Rotate = rot;
     m_control = new Control();
   }

I instantiate multiple Entity classes in another class like so:我在另一个类中实例化多个实体类,如下所示:

public class Environment
{

readonly Entity m_entity;
readonly Entity m_entity2;

public Environment()
{
   m_entity = new Entity(90.0f);
   m_entity2 = new Entity(180.0f);
}

Is there a way for me to determine what Control subclass the entity will use when instantiating it?有没有办法让我确定实体在实例化时将使用哪个 Control 子类?

Just pass your instance of Control via the constructor.只需通过构造函数传递您的Control实例。 Your Entity class will look like this:您的Entity类将如下所示:

readonly Control m_Control

public Entity(float rot, Control control)
   {
     Rotate = rot;
     m_control = control;
   }

Creating your Entity variables will look like this:创建您的Entity变量将如下所示:

m_entity = new Entity(90.0f, new PlayerControl());
m_entity2 = new Entity(180.0f, new AIControl());

This approach is called dependency injection.这种方法称为依赖注入。 See for example Wikipedia for more details.有关更多详细信息,请参见例如Wikipedia

Yes use generics:是的,使用泛型:

public class Entity<TControl> where TControl : Control, new()
{
   public float Rotate {get; private set;}

   readonly TControl m_Control;

   public Entity(float rot)
   {
       Rotate = rot;
       m_control = new TControl();
   }
}

public abstract class Control {}

public class PlayerControl : Control {}
public class AIControl : Control {}

public class Environment
{

    readonly Entity<PlayerControl> m_entity;
    readonly Entity<AIControl> m_entity2;

    public Environment()
    {
       m_entity = new Entity<PlayerControl>(90.0f);
       m_entity2 = new Entity<AIControl>(180.0f);
    }
}

The constraints will ensure that the generic type derives from Control and can be instantiated using a parameterless constructor.约束将确保泛型类型从Control派生,并且可以使用无参数构造函数进行实例化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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