简体   繁体   English

如何初始化动态创建的数组对象?

[英]How to initialize objects of array which has been created dynamically?

class bishop:unit {}
class knight:unit {}
class peasant:unit {}

void Battle(unit first, unit second, byte firstAmount, byte secondAmount)
{
  System.Array sideA = System.Array.CreateInstance(first.GetType(),firstAmount);
  for(int i=0; i< firstAmount; i++) 
  { 
   sideA[i] = ???
  }
}

In my last question I had problems with creating dynamic arrays and here is my next step problem! 在我的最后一个问题中,我在创建动态数组时遇到了问题,这是我的下一步问题! :D :D
Passable types to this method bishop, knight and etc 此方法的可传递类型主教,骑士等
Actually I don't understand how to initialize objects now. 其实我现在不明白如何初始化对象。 I can't type just sideA[i] = new first.GetType()(constructor parameters) and understand why, but I don't understand how to work around this 我不能只键入sideA [i] = new first.GetType()(构造函数参数)并理解原​​因,但是我不知道如何解决此问题

This is really, really bad design. 这确实是非常糟糕的设计。

I assume, that your method Battle could be an instance method of a class Game , which you didn't provide to us. 我认为,您的方法Battle可能是Game类的实例方法,而您没有提供给我们。

Then I strongly recommend, that the Battle method should NOT create instances of objects it works with. 然后,我强烈建议Battle方法不要创建与其一起使用的对象的实例。 It should only take them and do a battle action (compute lives, etc.). 它只应该带他们去做战斗动作(计算生命等)。

So, create these objects elsewhere and then just post them to the method. 因此,在其他位置创建这些对象,然后将它们发布到方法中。

class Game
{
    List<Bishop> bishops = new List<Bishop>() { new Bishop(..), ... };
    List<Knight> knights = new List<Knight>() { new Knight(..), ... };

    void Battle(List<Unit> first, List<Unit> second)
    {
        foreach(var f in first)
        {
            // get random unit from the second collection and attack him
            f.Attack(GetRandomKnight(second)); 
        }
    }

    public void StartBattle()
    {
        Battle(bishop, knights);
    }
}

Also make sure that you use correct C# naming. 另外,请确保使用正确的C#命名。 A name of a class should start with a capital letter. 班级名称应以大写字母开头。

class Unit
{
    public virtual void Attack(Unit enemy)
    {
        // default attack
        Kick(enemy);
    }

    protected Kick(Unit enemy) { ... }
}

class Bishop : Unit { }

Ondrej has a good answer. Ondrej有一个很好的答案。 Just to help you with arrays, you should never use reflection unless with a good reason. 只是为了帮助您使用数组,除非有充分的理由,否则不要使用反射。 I see no reason why you should be doing it here. 我认为您没有理由在这里这样做。 You can use the typical new keyword to instantiate an array. 您可以使用典型的new关键字实例化数组。

void Battle(unit first, unit second, byte firstAmount, byte secondAmount)
{
  var sideA = new unit[firstAmount];
  for(int i=0; i< sideA.Length; i++) 
  { 
    sideA[i] = ???
  }
}

If the instantiated array should really be of run time type of first , then you can rely on generics. 如果实例化的数组确实应该是first的运行时类型,则可以依赖泛型。

void Battle<T1, T2>(T1 first, T2 second, byte firstAmount, byte secondAmount) 
                   where T1 : unit where T2 : unit
{
  var sideA = new T1[firstAmount];
  for(int i=0; i< sideA.Length; i++) 
  { 
    sideA[i] = ???
  }
}

The totally dynamic way to settle things will be SetValue and GetValue : 解决问题的完全动态方式是SetValueGetValue

void Battle(unit first, unit second, byte firstAmount, byte secondAmount)
{
  var sideA = Array.CreateInstance(first.GetType(),firstAmount);
  for(int i=0; i< firstAmount; i++) 
  { 
   sideA.SetValue(???, i);
   sideA.GetValue(i); //to get the value back.
  }
}

Basically you dont get indexer syntax for System.Array . 基本上,您不会获得System.Array索引器语法。

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

相关问题 如何知道已创建哪个LiteDB版本DB文件? - How to know with which LiteDB version DB file has been created? 如何初始化已经在C#中声明的数组? - How to initialize an array which has already declared in C#? 如何保存动态创建的对象 - How to save dynamically created objects C#-如何为已动态创建的文本框编写离开事件 - C# - How to write a Leave Event for a textbox that has been created dynamically 如何获取已从.Net dll中的FoxPro自定义类创建的对象的属性? - How to get properties of object which has been created from FoxPro custom class in .Net dll? 如何访问和使用在WPF中的不同线程上创建的对象 - How to access & use an object which has been created on a different thread in WPF 如何在动态生成按钮时检索哪个按钮被单击 - How to retrieve which Button has been Clicked when Its Generated Dynamically 当动态生成按钮时,如何判断该按钮已被单击? (MVVM) - How to tell which Button has been clicked, when it's generated dynamically? (MVVM) 使用通过ILMerge创建的库中的程序集 - Use assembly from a library which has been created through ILMerge 在运行时创建的哪个切换已在Unity中调用? - Which Toggle created at runtime has been called in Unity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM