【问题标题】:Aggregation Inheritance in Arrays数组中的聚合继承
【发布时间】: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


【解决方案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];

【讨论】:

  • 好的,我明白了,但你看到数组也有停滞的对象(岩石树木植物)。基本上我想包括一个计时器来移动数组中的动物,如果我遇到一个非停滞的对象,我必须检查条件。
【解决方案2】:

可能Object是保留字。

尝试将Object 更改为ForestObject

【讨论】:

  • “可能Object是保留字。”?不,可能根本不是这样。试试看。
猜你喜欢
  • 2012-01-31
  • 1970-01-01
  • 1970-01-01
  • 2012-12-08
  • 2012-01-06
  • 2014-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多