简体   繁体   English

初始化对象时调用方法

[英]Calling a method when initializing object

I'm looking at a way of cleaning up some code. 我正在寻找一种清理一些代码的方法。 I have the below 我有以下

  var sprite1 = new Sprite(_content.Load<Texture2D>("Content/Block.png"));
  sprite1.Position = new Vector2f(100, 100);
  sprite1.SetInput(Key.A, Key.D, Key.W, Key.S);

  var sprite2 = new Sprite(_content.Load<Texture2D>("Content/Block.png"));
  sprite2.Position = new Vector2f(200, 100);
  sprite2.SetInput(Key.Left, Key.Right, Key.Up, Key.Down);

  _sprites = new List<Sprite>()
  {
    sprite1,
    sprite2,
  };

I'd like to initialize the "Sprite" when adding it to the list. 我想在将“ Sprite”添加到列表时进行初始化。 But the problem I'm having is the "SetInput" method. 但是我遇到的问题是“ SetInput”方法。 That method is optional. 该方法是可选的。

I know this isn't syntactically correct, but below is the sort of thing I'm talking about 我知道这在语法上是不正确的,但是下面是我在说的那种事情

  _sprites = new List<Sprite>()
  {
    new Sprite(_content.Load<Texture2D>("Content/Block.png")) { Position = new Vector2f(100, 100), SetInput(Key.A, Key.D, Key.W, Key.S) },
  };

As you can see I'm attempting to call a method in a similar to way you can set properties. 如您所见,我正在尝试以类似于设置属性的方式来调用方法。

Here are the ways I'm thinking of getting around it 这是我正在考虑的解决方法

  1. Optional parameters constructor for the keys 键的可选参数构造函数

     public Sprite(Texture2D texture, Key? left, Key? right, Key? up, Key? down) 
  2. A second constructor with the keys 第二个带有键的构造函数

     public Sprite(Texture2D texture, Key left, Key right, Key up Key down) 
  3. Creating a delegate in the "Sprite" class that is assigned when initialized, and then loop around all the _sprites, calling the method 在初始化时分配的“ Sprite”类中创建一个委托,然后在所有_sprite周围循环,调用方法

The code I've put at the start is fine, I'm just wondering if there is a 'better' way? 一开始我写的代码很好,我只是想知道是否有“更好”的方法?

Thanks. 谢谢。

You can also use so called "fluent" interface: 您还可以使用所谓的“流利”界面:

public class Sprite {
     public Sprite SetPosition(Vector2f vector) {
         // set
         return this;
     }

     public Sprite SetInput(Key left, Key right, Key up Key down) {
         // set
         return this;
     }
}

Which can be used like this 可以这样使用

 _sprites = new List<Sprite>()
  {
      new Sprite(_content.Load<Texture2D>("Content/Block.png"))
          .SetPosition(new Vector2f(100, 100))
          .SetInput(Key.A, Key.D, Key.W, Key.S)
  };

Whether it is "better" is a matter of taste, but it's definetly an option. 是否“更好”取决于口味,但这绝对是一种选择。

What about creating Input class to cleanup it? 创建Input类进行清理呢?

internal class Sprite
{
    public Input Input { get; set; }
    ... 
}

internal class Input
{
    public Key Left { get; private set; }
    public Key Right { get; private set; }
    public Key Up { get; private set; }
    public Key Down { get; private set; }

    public Input(Key left, Key right, Key up, Key down)
    {
        Left = left;
        Right = right;
        Up = up;
        Down = down;
    }
}

then you'll get exactly what you want 那你就会得到你想要的

_sprites = new List<Sprite>()
{
    new Sprite(_content.Load<Texture2D>("Content/Block.png")) 
    { 
         Position = new Vector2f(100, 100), 
         Input = new Input(Key.A, Key.D, Key.W, Key.S),
    },
 };

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

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