(Java RPG) in if statement

I created 4 classes (Mage, Rouge, Warrior & dool) that extend the 'Character' class. Each of them has its own combat methods within its classes. However, since only one of these objects is built inside the if / else statement, java does not recognize them as existing. The problem is that I need to call a class method different from the if / else statement. I tried to initialize 4 objects as null first, then construct / overwrite them, but when I call the method, it still refers to them as zero. The formatting is a bit confusing, but here:

EDIT: Thanks for the tip!

public class Rpg {
    public static Warrior wplayer = null;
    public static Rouge rplayer = null;
    public static Mage mplayer = null;
    public static Fool fplayer = null;

    public static void main(String[] args){
        a2 = scan.nextInt();
        if (a2 == 1){
            Warrior wplayer = new Warrior();
        } else if (a2 == 2) {
            Rouge rplayer = new Rouge();
        } else if (a2 == 3) {
            Mage mplayer = new Mage();
        } else {
            Fool fplayer = new Fool();
            while (!notdone){
                System.out.println("1: Arena");
                System.out.println("2: Blacksmith");
                System.out.println("3: Shop");
                System.out.println("4: Leave town");
                System.out.println("5: Save and Quit");
                int choice = scan.nextInt();
                if (choice == 1 && wplayer != null){
                    wplayer.fightEnemy();
                } else if (choice == 1 && rplayer != null){
                    rplayer.fightEnemy();
                } else if (choice == 1 && mplayer != null){
                    mplayer.fightEnemy();
                } else {
                    fplayer.fightEnemy();
                }
            }
        }
    }   
}
+4
source share
3 answers

if/else. , :

Character player = null;

if (a2==1){
    player = new Warrior();
} else if (a2==2){
    player = new Rouge();
} else if (a2==3){
    player = new Mage();
} else{
    player = new Fool();
}

// by now, player was instantiated by some concrete class and you can use it

fightEnemy(), :

 ...
 if (choice==1){
        // java will figure out which "Character" this really is
        // then it will call the fightEnemy method for that specific "Character" type
        player.fightEnemy();
 }
 ...
+3

if (a2==1){
    Warrior wplayer = new Warrior();
}

if (a2==1){
       wplayer = new Warrior();
}
0

.

  • {}. , , if(a2 == 1) { Warrior wplayer = new Warrior(); }, wplayer {} if...else . , Warrior wplayer if...else if...else:

    wplayer = null;

    if (a2 == 1) {    wplayer = (); }

    // wplayer

  • When adding, you should look at the structure of your code. You have several types Player. All players have a specific implementation fightEnemy()and select the correct instance that you are using if...else. Imagine if you add another type later Player, then you have to change all of these if...else. This is not good. Let me give an example of how this can be done.

Example

interface Player {
    void fightEnemy();
}

class Warrior implements Player {
    public void fightEnemy() { }
}

class Rouge implements Player {
    public void fightEnemy() { }
}

class Mage implements Player {
    public void fightEnemy() { }
}

class Fool implements Player {
    public void fightEnemy() { }
}

Scanner scan = new Scanner(System.in);
int a2 = scan.nextInt();

Player player;

if (a2 == 1) {
    player = new Warrior();
} else if (a2 == 2) {
    player = new Rouge();
{ else if (a2 == 3) {
    player = new Mage();
} else {
    player = new Fool();
}

System.out.println("1: Arena");
System.out.println("2: Blacksmith");
System.out.println("3: Shop");
System.out.println("4: Leave town");
System.out.println("5: Save and Quit");

int choice = scan.nextInt();

if (choice == 1) {
    player.fightEnemy();
}
0
source

Source: https://habr.com/ru/post/1686369/


All Articles