如何检查 object 是否已经创建?

How to check if an object has been created yet?

问题

我有一支军队 class 是 运行 两次(通过单独的 driver class)创建两个军队。在那个 Army class 中,有以下语句可以从我拥有的其他 class 文件创建新的 objects:

  public final Archer archer = new Archer(this);
  public final Catapult catapult = new Catapult(this);
  public final NCatapult ncatapult = new NCatapult(this); //Ninja catapult
  public final Ninja ninja = new Ninja(this);
  public final Horse horse = new Horse(this);
  public final Samurai samurai = new Samurai(this);

每个 object 的构造函数都需要在其构造函数方法参数中使用 "Army" object,因此实例化中使用了 this 关键字。

所以。在 Horse class 的一个方法中,我创建了一个新的 "Warrior" object,因为 Warrior 最初没有在 Army class.

中实例化
private boolean warriorCreated = false;
  public void dismount(){
    if (!warriorCreated){
      Warrior warrior = new Warrior(myArmy);
      warriorCreated = true;
      myArmy.getOurArmyWindow().getTabbedPane().addTab("(Ethan)", new ImageIcon("images/warriorIcon.jpg"), warrior.genGetPanel()); //This is just adding the Warrior's tab to my GUI. It's not relevant to the problem.
    }
    ...//Some stuff to handle when the method is called and warriorCreated is true
    }//end dismount()

您会注意到它如何在 Warrior 的构造函数中使用变量 "myArmy"。这就是实例化 Horse 时传递的 Army object。

public Horse(Army armyInstance){ //A constructor that takes in whatever Army object called it
    myArmy = armyInstance;
  }

在所有不同的实例化 object 中,我可以使用 myArmy.getEnemyArmyInstance() 方法访问其他敌对军队。我不想详细解释它是如何工作的,并且比我现在占用更多 space,所以请相信我,它是有效的。

这意味着通常我可以通过调用 Ninja enemyNinja = myArmy.getEnemyArmyInstance().ninja; 之类的东西来引用由对方军队实例化的 object (我可以将 "Ninja" 替换为任何其他 object 我想参考)。

但是,因为战士 object 是由马 class 方法实例化的,而不是从每个军队 object 中的 get-go 实例化的,我首先要检查如果它在尝试从单独的 object.

引用它之前已经被实例化

这就是我运行遇到麻烦的地方。每当我尝试检查 Warrior object 是否已实例化时,它都会在 compile-time.

处给我一个错误

例如,考虑这个检查:

public void checkForWarrior(){
if (myArmy.getEnemyArmyInstance().warrior != null){
  System.out.println("A warrior exists!");
}
else
  System.out.println("There is no warrior!");
}

它抛出错误

error: cannot find symbol   
if (myArmy.getEnemyArmyInstance().warrior != null){
                                 ^
symbol:   variable warrior
location: class Army

我试过的

我认为创建的 Warrior object 可能不是 Army 的成员(这是正确的术语,对吧?),所以我尝试在行中添加“.horse”只是为了得到类似的结果结果。

   if (myArmy.getEnemyArmyInstance().horse.warrior != null){
                                          ^
   symbol:   variable warrior
   location: variable horse of type Horse

我什至尝试添加一个布尔值,当 Warrior 被实例化时,它会在 Horse class 中打开,然后首先检查它。但是没有骰子。

private boolean mounted = true; //This is in the Horse class and is set to
false when a Warrior is instantiated.

在其他 classes:

if (myArmy.getEnemyArmyInstance().horse.getMounted()){
Warrior enemy = myArmy.getEnemyArmyInstance().warrior;//Still gives me an
//error that it cannot find the variable warrior. (I tried adding .horse to
//this, too).

问题

来自 object 不是实例化它的 Horse,我如何检查一个 Warrior object 是否已经实例化,以便我可以从其他 object 引用它 objects?

如果我对你的问题的理解正确,你需要将以下内容添加到你的 Army class:

public Warrior warrior;

然后在你的 Horse class:

  public void dismount(){
    if (myArmy.warrior == null){
      myArmy.warrior = new Warrior(myArmy);
      myArmy.getOurArmyWindow().getTabbedPane().addTab("(Ethan)", new ImageIcon("images/warriorIcon.jpg"), warrior.genGetPanel()); //This is just adding the Warrior's tab to my GUI. It's not relevant to the problem.
    }
    ...//Some stuff to handle when the method is called and warriorCreated is true
    }//end dismount()

希望对您有所帮助。