【发布时间】:2017-11-23 10:18:20
【问题描述】:
我在尝试创建 Forest 类型的数组时遇到问题。 这片森林包含动物和物体。 (聚合) 动物是 X 或 Y 对象是 A 或 B 我为森林创建了一个类 我为 Animals & Objects 创建了类。 我的问题从这里开始。
public class Forest
{
private AnimalType animals;
private ObjectType stagnantObject;
Forest[,] forestArray;
Random rand = new Random();
public enum AnimalType
{
Lion = 1, Elephant = 2, Deer = 3
}
public enum ObjectType
{
Rock = 1, Plant = 2, Tree = 3
}
public Forest(AnimalType animal)
{
animal = animals;
}
public Forest()
{
}
public void createArray(int w, int h)
{
forestArray = new Forest[w, h];
}
public int[] randomPosition()
{
int upperbound = forestArray.GetUpperBound(0);
int lowerbound = forestArray.GetUpperBound(1);
int xCord;
int yCord;
xCord = rand.Next(0,upperbound);
yCord = rand.Next(0,lowerbound);
int [] array = new int [2];
array[0] = xCord;
array[1] = yCord;
return array;
}
public Object initialzeArray(int l, int d, int e, int r, int t, int p)
{
int numOfLion = l;
while(numOfLion != 0 )
{
int[] random = randomPosition();
int x = random[0];
int y = random[1];
forestArray[x, y] = new Lion(); // this is wrong! How do i implement it correctly?? ***
numOfLion--;
}
如果有人能澄清什么是错的,我已经尝试了很多代码,但我总是遇到问题,我的理解存在缺陷。因此,如果有人可以将我链接到可以帮助我的文章或要阅读的内容,那么我已经阅读了很多书,但我一无所获。还有这个问题叫什么??我很难想出这个问题的标题 感谢您的帮助,谢谢
*****编辑 这是错误。 试图以与数组不兼容的类型访问元素。 ***** 编辑 在初始化森林时,您需要在空白空间中随机分配一些大象、狮子、鹿、岩石、树木和植物。这些数字由用户指定。
所以我有一个 Forest 数组,它有一个对象类型为 animal 和 Stagnant Object。 我担心我没有使用正确的术语。所以请随时修正我的词汇量。 另一个问题,我对 AnimalType 和 ObjectType 的减速是否在正确的类中?
我有 1 个森林类、2 个抽象类和 6 个具体类。
但是你如何编码聚合?
【问题讨论】:
-
你的类定义在哪里?
-
为什么你说岩石、树木和植物停滞不前? stangnant:(指水体或密闭空间的大气)没有水流或流动,因此经常有难闻的气味。
-
@Enigmativity 嘿伙计,基本上类定义非常简单。抽象的动物类和具体的子类,还有停滞不动的定义,停滞的定义就是你刚才说的。别担心,我检查过了。
-
我看到的另一个定义是不活跃、迟钝、缓慢、缓慢移动、昏昏欲睡、静止、平淡、沮丧、安静、沉闷、衰落、垂死、垂死、死亡、休眠、停滞。我不会说岩石、树木和植物停滞不前。
-
无论如何,如果定义很简单,请在您的问题中发布。它使我们更容易回答您的问题。
标签: c# arrays aggregation composition