简体   繁体   中英

How can I access variable from another class?

I have made an object of my class Commands

Commands commands = new Commands();

but, I need to access the array inside that method, not sure how to do it.

Player.java

package com.PenguinGaming;

import java.util.Scanner;

public class Player {

public void User(){

    Commands commands = new Commands();

    int Maxlife = 25;
    int life = 25;
    int turns = 0;

    //Starting player location
    int playerX = 4;
    int playerY = 4;

    Scanner scanner = new Scanner(System.in);

    String input = null;


    while(life > 1){

        System.out.println("Player life: " + life + "/" + Maxlife);
        input = scanner.nextLine();
        input = input.toLowerCase();

        life -= 1;

        for(int i = 0; i == 30; i++){
            turns++;
            if(turns == 30){
                Maxlife += 5;
            }
        }

        if (input.equals(commands[0])) {
        // give description for player location
        System.out.println(locations[playerX][playerY]);
        }

        if(input.equals(commands[1])){
            //Go north
            System.out.println(locations[playerX][playerY+1]);
            playerY += 1;
        }

        if(input.equals(commands[2])){
            //Go east
            System.out.println(locations[playerX+1][playerY]);
            playerX += 1;
        }

        if(input.equals(commands[3])){
            //Go south
            System.out.println(locations[playerX][playerY-1]);
            playerY -= 1;
        }

        if(input.equals(commands[4])){
            //Go west
            System.out.println(locations[playerX-1][playerY]);
            playerX -= 1;
        }

        if(input.equals(commands[5])){
            break;
        }

        if(life <= 0){
            System.out.println("Looks like you have starved, better luck next game");
            break;
        }
        else
            System.out.println("You can not move in that direction");

    }
    System.exit(0);
}
}

Commands.java

package com.PenguinGaming;

public class Commands {

/*
 * Command list:
 * - investigate (advanced description)
 * - go north
 * - go south
 * - go west
 * - go east
 * - pick up
 * - eat
 * - drink from
 * - drink object
 * - climb up/down
 * - burn
 * - use object
 * - attack
 * - defend
 * - description (basic description)
 * - read
 * - life
 * - help (brings up command list)
 */

public void commandlist(){

    String[] commands = new String[6];
    commands[0] = "investigate";
    commands[1] = "go north";
    commands[2] = "go east";
    commands[3] = "go south";
    commands[4] = "go west";
    commands[5] = "terminate game";
}

}

If you want to clean your code up a little bit you can do something like this:

public String[] commandlist()
{
  return new String[]{"investigate", "go north", "go east", "go south", "go west", "terminate game"};
}

Java has already way of storing well defined set of constant values. It's called enum

public enum Commands {
    INVESTIGATE("investigate"),
    GO_NORTH("go north"),
    GO_EAST("go east"),
   ...

}

and so on.

This way you can import Commands to class you write and get all of its values by Commands.values() that returns set, so you can iterate over it, whatever you want. This way if you find a need of adding another command you have to do it in one place only because you operate on, check for equality on those values, etc.

I forgot to mention that by doing so, you don't have to create object Commands manually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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