Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 数组中的聚合继承_C#_Arrays_Aggregation_Composition - Fatal编程技术网

C# 数组中的聚合继承

C# 数组中的聚合继承,c#,arrays,aggregation,composition,C#,Arrays,Aggregation,Composition,我在尝试创建类型为Forest的数组时遇到问题。 这片森林既有动物也有物体。(汇总) 动物不是X就是Y 对象是A或B 我为forest创建了一个类 我为动物和对象创建了类。 我的问题从这里开始 public class Forest { private AnimalType animals; private ObjectType stagnantObject; Forest[,] forestArray; Random rand = new Random()

我在尝试创建类型为Forest的数组时遇到问题。 这片森林既有动物也有物体。(汇总) 动物不是X就是Y 对象是A或B 我为forest创建了一个类 我为动物和对象创建了类。 我的问题从这里开始

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个具体类


但是如何编写聚合

可能是
对象
是一个保留字

尝试将
Object
更改为
ForestObject

排队

Forest[,] forestArray;
您正在将
forestArray
声明为
Forest
的数组,但此处

forestArray[x, y] = new Lion(); 
您想添加一个
Lion
实例
Lion
似乎不是源于
Forest
而是源于
Animal
。(
Forest
无论如何没有多大意义,因为它是包含数组的类)。尝试将
forestArray
声明为

Animal[,] forestArray;
改变

forestArray = new Forest[w, h];


“可能该对象是保留字。”?不,可能根本不是这样。试试看。你的类定义在哪里?为什么你把石头、树木和植物叫做停滞?Stangnant:(指水体或密闭空间的大气)没有水流或水流,因此经常有难闻的气味。@Enigmativity嘿,伙计,基本上类的定义很简单。抽象的动物类和具体的子类,如果停滞是非移动的,那么停滞的定义就是你刚才所说的。别担心,我查过了。我看到的另一个定义是不活跃、迟钝、缓慢、行动迟缓、无精打采、静止、平淡、沮丧、安静、迟钝、衰弱、垂死、死亡、休眠、停滞。我不会说岩石、树木和植物是停滞的。不管怎样,如果定义很简单,请在你的问题中发布它们。这让我们更容易回答你的问题。好吧,我明白了,但是你看,这个阵列也有静止的物体(岩石、树木和植物)。基本上,我想包括一个计时器,移动阵列中的动物,如果我遇到一个非停滞物体,我必须检查条件。
forestArray = new Animal[w, h];