简体   繁体   English

在C#中返回模糊对象

[英]Return ambiguous object in C#

i am currently making a game in my first year of college. 我目前在大学一年级就在做游戏。 I have four enemy objects (Orc, Elf, Goblin, and reptilian) i want to create objects of each enemy depending on the level and room the player is currently in and return object array of that particular enemy. 我有四个敌人对象(兽人,精灵,地精和爬虫类动物),我想根据玩家当前所在的等级和房间为每个敌人创建对象,并返回该特定敌人的对象数组。 I get the error "the name "enemies" does not exist in the current context". 我收到错误消息“当前上下文中不存在名称”敌人”。 Thank you in advance. 先感谢您。

public Object[] level(int lev, int room){
                int enemiesToCreate = 2;
                enemiesToCreate += lev += room;
                if (lev == 1)
                {
                    Goblen[] enemies = new Goblen[enemiesToCreate];
                }
                else if (lev == 2)
                {
                    Orc[] enemies = new Orc[enemiesToCreate];
                }
                else if (lev == 3)
                {
                    Elf[] enemies = new Elf[enemiesToCreate];
                }
                else if (lev == 4)
                {
                    Reptilian[] enemies = new Reptilian[enemiesToCreate];
                }

                // error here
                return enemies;

            }

You must define enemies outside of the if blocks. 您必须在if块之外定义enemies When you enclose something with {} brackets, it basically creates an additional scope level, typically called a block, that outer levels will not see. 当用{}括起来时,它基本上会创建一个附加的作用域级别,通常称为块,外部级别将看不到。 So for instance, the following will not work: 因此,例如,以下操作将无效:

if (lev == 1)
{
  Goblen[] enemies = new Goblen[enemiesToCreate];
}

return enemies;

However, if we declare enemies outside of the if block, the return statement will be able to "see" the declaration. 但是,如果我们在if块之外声明敌人,则return语句将能够“看到”该声明。 For example: 例如:

object[] enemies;

if (lev == 1)
{
   enemies = new Goblen[enemiesToCreate];
}

return enemies;

Note that since you are using differing return types, you must use a common base type for the return variable. 请注意,由于您使用的返回类型不同,因此必须对返回变量使用通用的基本类型。 object always works, though something more specific would be preferable. object始终有效,尽管更具体的方法是更可取的。

Additionally, in this particular situation, you would be better off just skipping the return variable completely and just directly returning from within the if statement, like so: 此外,在这种特殊情况下,最好完全跳过return变量,而直接从if语句中直接返回,如下所示:

public Object[] level(int lev, int room){
  int enemiesToCreate = 2;

  enemiesToCreate += lev += room;

  if (lev == 1)
  {
    return new Goblen[enemiesToCreate];
  }
  else if (lev == 2)
  {
    return new Orc[enemiesToCreate];
  }
  ...
  throw new Exception("Unknown enemy type");
}

Make classes Goblen, Orc, Elf, Reptilian inherited from an abstract class or an interface. 使类Goblen, Orc, Elf, Reptilian从抽象类或接口继承。 Declare enemies of that type outside if statements. if语句之外声明该类型的敌人。 Probably you will find rewarding the implementation of abstract factory design pattern for your case. 也许您会发现针对您的案例实施抽象工厂设计模式会有所收获。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM