[英]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
: 解决问题的完全动态方式是
SetValue
和GetValue
:
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.