我在尝试创建一个类型为Forest的数组时遇到了问题。 这个森林包含两个动物&对象。 (Aggregation) 动物是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--;
}
请,如果有人能阐明什么是错的,我已经尝试了许多代码,但我总是一个问题结束了,我在我的理解的缺陷。所以如果任何人都可以将我链接到一篇文章或某些可以帮助我的文章,我已经阅读了很多,而且我没有得到任何地方。还有什么叫这个问题?我有困难试图拿出这个问题的标题 帮助表示赞赏谢谢
*****编辑 这是错误。 试图将元素作为与数组不兼容的类型进行访问。 *****编辑 在初始化森林时,您需要随机分配空闲空间中的一些大象,狮子,鹿,岩石,树木和植物。这些数字由用户指定。
所以我有一个森林数组,有一个对象类型的动物,和停滞对象。 我担心我没有使用正确的术语。所以请随时解决我的词汇量。 另一个问题,是我在正确的类中减速了AnimalType & ObjectType?
我有1个森林类,2个抽象类和6个具体类。
但你如何编码聚合?
在哪里你的类定义? – Enigmativity
为什么你称石头,树木和植物停滞不前?斯坦南特:(水体或密闭空间的气氛)没有电流或流动,因此往往具有不愉快的气味。 – Enigmativity
@Enigmativity嘿人,基本上这个类的定义很简单。抽象的动物类和具体的子类,也定义如果停滞不动,停滞的定义就是你刚才所说的。不要担心我检查它。 –