所以我在这里有一些代码:
public class World
{
private static World _world = null;
public static World world
{
get
{
if (_world == null)
_world = new World();
return _world;
}
}
private Enemy[] Monsters = new Enemy[2];
public Enemy[] monsters
{
get { return Monsters; }
}
private Sword[] Swords = new Sword[2];
public Sword[] sword
{
get { return Swords; }
}
public void PopulateEnemies()
{
Enemy Goblin = new Enemy("Goblin", "Looks dirty and not so friendly", 100);
}
public void PopulateItems()
{
Sword Excalibur = new Sword("Excalibur", 100);
}
}
这是我的Main()
方法:
static void Main()
{
World.world.PopulateItems();
World.world.PopulateEnemies();
Player player = new Player("", 50, 50, 1, 0, 100);
DisplayText("Welcome warrior, what is your name? ");
player.Name = Console.ReadLine();
if (player.Name == string.Empty)
{
DisplayText("You don't want to tell me your name, all right... I'll call you Norman!");
}
else
{
DisplayText("Nice to meet you {0}, what are you waiting for, go kill some monsters!", player.Name);
}
Console.ReadLine();
Console.WriteLine();
DisplayText("You find a nice sword in a cavern. You pick it up!");
World.world.sword[0].PickUp();
World.world.monsters[0].EnemyEncounter();
}
现在,当我到达PickUp()
方法时,它会抛出异常,但我已经在另一个类中创建了一个新的Sword,并使用PopulateItems
来确保我的Main()
得到它。现在,我做错了什么?