如何在其范围之外使用对象

时间:2015-01-07 05:30:16

标签: java

即时创建一个口袋妖怪对战游戏,我根据用户输入在if语句中实例化pokemon对象。这显然是一个问题,因为我不能在if语句之外使用这些对象,但必须在每个用户上创建不同的pokemon对象。我需要在if语句之外使用这些对象及其方法的一般方法,或者我需要另一种方法来创建不同于用户输入的对象。请记住,我是java的初学者,谢谢,我需要帮助,任何人的帮助将不胜感激。

代码很长,但这里有一段。

   if(answers == 1) {
      Gible pokemon1 = new Gible("Gible","Gabite & Garchomp", "Achieving Level 24","Dragon Type","Ground Type", "Slash, Tackle, Sandtomb, Dragonclaw", "Monster & Dragon", true , "Gabite & Garchomp", "Slash!", "Tackle!" , "Sandtomb!", "Dragonclaw!", 239, 189, 179, 189, 183, 320);
      System.out.println(pokemon1);
     }
     else {
      Squirtle pokemon2 = new Squirtle("Squirtle","Wartortle & Blastoise", "Achieving Level 16","Water", "None", "Tackle, Water Gun, Hydro Pump, Skull Bash", "Monster & Water 1", true , "Wartortle & Blastoise", "Tackle!", "Water Gun!" , "Hydro Pump!", "Skull Bash!", 195, 229, 199, 227, 185, 292);
      System.out.println(pokemon2);  

       }

if语句只是用户输入决定他们想要哪个口袋妖怪(#1或#2)。我创建的对象直接绑定到抽象类的子类,该类返回一堆stats和info(基本上我放入参数中的东西)。

3 个答案:

答案 0 :(得分:3)

您可以在if语句之外声明对象,然后将其初始化。所以不要这样:

if (some test) {
    PokemonObject po = new PokemonObject();
    // ... use po
} else if (another test) {
    PokemonObject po = new PokemonObject();
    // ... use po
} else if (a third test) {
    PokemonObject po = new PokemonObject();
    // ... use po
}
// try to use po (doesn't compile)

请改用:

PokemonObject po = null;
if (some test) {
    po = new PokemonObject();
    // ... use po
} else if (another test) {
    po = new PokemonObject();
    // ... use po
} else if (a third test) {
    po = new PokemonObject();
    // ... use po
}
if (po != null) {
    // try to use po (works!)
}

编辑:如果你有专门的pokemon对象类,那么你可以定义一个基类(类可以扩展)或一个接口(类可以实现),定义所有pokemon对象共有的行为。您的代码可能如下所示:

PokemonObject po = null; // base class type for Gibble and Squirtle
if (answers == 1) {
    Gible pokemon = new Gible("Gible","Gabite & Garchomp", "Achieving Level 24","Dragon Type","Ground Type", "Slash, Tackle, Sandtomb, Dragonclaw", "Monster & Dragon", true , "Gabite & Garchomp", "Slash!", "Tackle!" , "Sandtomb!", "Dragonclaw!", 239, 189, 179, 189, 183, 320);
    // do Gible-specific stuff here
    po = pokemon;
} else {
    Squirtle pokemon = new Squirtle("Squirtle","Wartortle & Blastoise", "Achieving Level 16","Water", "None", "Tackle, Water Gun, Hydro Pump, Skull Bash", "Monster & Water 1", true , "Wartortle & Blastoise", "Tackle!", "Water Gun!" , "Hydro Pump!", "Skull Bash!", 195, 229, 199, 227, 185, 292);
    // do Squirtle-specific stuff here
    po = pokemon;
}
// do general Pokemon stuff here
System.out.println(pokemon.getName());

如果使用基类而不是接口,基类构造函数(或构造函数)将只接受对所有Pokemon对象有意义的参数。它也只会定义所有Pokemon对象共有的操作(如getName())。

另外,构造函数调用看起来非常复杂且容易出错。您可能希望使用builder pattern或相关技术来避免使用too many parameters的构造函数。

答案 1 :(得分:0)

试试这个:

public Gible pokemon1;
public Squirtle pokemon2;
    if(answers == 1) {
          this.pokemon1 = new Gible("Gible","Gabite & Garchomp", "Achieving Level 24","Dragon Type","Ground Type", "Slash, Tackle, Sandtomb, Dragonclaw", "Monster & Dragon", true , "Gabite & Garchomp", "Slash!", "Tackle!" , "Sandtomb!", "Dragonclaw!", 239, 189, 179, 189, 183, 320);
          System.out.println(pokemon1);
         }
         else {
          this.pokemon2 = new Squirtle("Squirtle","Wartortle & Blastoise", "Achieving Level 16","Water", "None", "Tackle, Water Gun, Hydro Pump, Skull Bash", "Monster & Water 1", true , "Wartortle & Blastoise", "Tackle!", "Water Gun!" , "Hydro Pump!", "Skull Bash!", 195, 229, 199, 227, 185, 292);
          System.out.println(pokemon2);  

           }

现在你可以从每个地方调用这两个变量。

答案 2 :(得分:0)

由于Ted Hopp建议而不是在IF块内部进行声明和实例化,您可以声明IF块之外的对象,当您进入IF块时,您可以实例化对象。

GibleClass gb = null;
SquirtleClass sq = null;

        if(answer == 1){
        gb = new Gible("Gible","Gabite & Garchomp", "Achieving Level 24","Dragon Type","Ground Type", "Slash, Tackle, Sandtomb, Dragonclaw", "Monster & Dragon", true , "Gabite & Garchomp", "Slash!", "Tackle!" , "Sandtomb!", "Dragonclaw!", 239, 189, 179, 189, 183, 320);
        System.out.println(gb);

        } else {
        sq = new Squirtle("Squirtle","Wartortle & Blastoise", "Achieving Level 16","Water", "None", "Tackle, Water Gun, Hydro Pump, Skull Bash", "Monster & Water 1", true , "Wartortle & Blastoise", "Tackle!", "Water Gun!" , "Hydro Pump!", "Skull Bash!", 195, 229, 199, 227, 185, 292); 
        System.out.println(sq);
        }