我在尝试创建类型为Forest的数组时遇到问题。 这片森林包含动物和动物。对象。 (聚合) 动物是X或Y. 对象是A或B. 我为森林创建了一个类 我为动物和动物创建了课程。对象。 我的问题从这里开始。
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数组,它有一个对象类型动物和Stagnant Object。 我担心我没有使用适当的术语。所以请随意修复我的词汇。 另一个问题是,我对AnimalType& amp; ObjectType在正确的类中?
我有1个森林类,2个抽象类和6个具体类。
但是你如何编码聚合?
答案 0 :(得分:1)
在第
行Forest[,] forestArray;
您已将forestArray
声明为Forest
的数组,但此处
forestArray[x, y] = new Lion();
您想要添加Lion
个实例。 Lion
似乎不是来自Forest
,而是来自Animal
。 (Forest
无论如何都没有多大意义,因为它是包含数组的类)。尝试将forestArray
声明为
Animal[,] forestArray;
并更改
forestArray = new Forest[w, h];
到
forestArray = new Animal[w, h];
答案 1 :(得分:-2)
可能Object
是保留字。
尝试将
Object
更改为ForestObject