如何从另一个方法访问变量

时间:2015-02-08 13:49:09

标签: java scope

我有一个名为" Game"在爪哇,目标/目标是到达某个房间,宝座"房间。当到达王座室时,游戏结束。

public class Game {

    // fields

    private Room currentRoom;
    private boolean finished;
    private Room throne;

    /**
     * Create the game and initialise its internal map.
     */
    public Game() 
    {
        finished = false;
        createRooms();
    }

    /**
     * Create all the rooms and link their exits together.
     */
    private void createRooms()
    {
        Room gate, graveyard, church, crypt, entrance, hall, kitchen, buttery, greathall, staircase,
        dungeon, topstaircase, throne, solar, wardrobe, privy;

        // create the rooms
        gate = new Room("outside the old gate of the castle");
        graveyard = new Room("on a wind-swept gaveyard");
        church = new Room("in a small ancient church");
        throne = new Room("in the throne room with golden walls");
        // other rooms ...

        // initialize room exits

        gate.setExit("north", graveyard);
        throne.setExit("south", topstaircase);
        // other exits ...
    }
}

走向一个方向/房间:

public String goRoom(String direction) 
{
    assert direction != null : "Game.goRoom gets null direction";

    // Try to leave current room.
    Room nextRoom = currentRoom.getExit(direction);
    if (nextRoom == null) {
        return "There is no exit in that direction!";
    } else {
        currentRoom = nextRoom;
        return currentRoom.getLongDescription();
    }
}

进入一个特定的房间结束游​​戏,即宝座"这里的房间我做了什么:

    if (currentRoom.equals(throne)) {
        finished = true;
        return "Congrats you have reached the throne room";
    }

但我一直收到此错误:cannot find symbol - variable throne

这是一个基于" Object的项目,首先使用Java,使用blueJ"

3 个答案:

答案 0 :(得分:4)

您可以将其声明为private void createRooms(),而不是在Room throne;中声明房间宝座,因此您可以在构造函数后面说createRooms,然后您可以在{{1}中初始化它方法。

您可以考虑将其设为final因为它不再发生变化而且明确说明了意图

在这种情况下,我不会将它传递给下一个参数,对我来说将它作为一个字段是有意义的

编辑:关于您无法访问的声明评论的主题,我怀疑您正在做的是在以下代码之后检查游戏的完成状态:

} else {
        currentRoom = nextRoom;
        return currentRoom.getLongDescription();
    }

因为你已经从这里的方法返回,所以你永远不会真正检查游戏的最终状态。

答案 1 :(得分:1)

您将Room存储为本地变量。 Room将消失。

要解决此问题,您应该在类级别声明变量:

public class Game {
    private Room currentRoom;
    private boolean finished;
    private Room Goal;
    private Map<String, Room> rooms;


public Game() {
    finished = false;
    rooms = new HashMap<>();
    createRooms();
}

//....

private void createRooms() {
    rooms.put("gate", new Room("outside the old gate at the castle"));
    //similar for other rooms
    //...
}

然后,您可以通过定义功能

来访问您的房间
public Room getRoom(String roomName) {
    if (rooms.containsKey(roomName))
        return rooms.get(roomName);
    throw new IllegalArgumentException("No such room: " + roomName);
}

例如:

if (currentRoom.equals(getRoom("throne")) {

        finished = true;
        return "Congrats you have reached the throne room";
}

如果goRoom()不在Game,您需要先获取Game的实例,然后在该实例上调用getRoom()

答案 2 :(得分:1)

  

但我一直收到这个错误:&#34;找不到符号 - 变量   即位&#34 ;.我相信我需要一个地方,但我不确定在哪里和   这是多么有用。

您收到此错误,因为变量throne是一个局部变量,其范围就在声明的方法内部

private void createRooms()
{
    Room gate, graveyard, church, crypt, entrance, hall, kitchen, buttery, greathall, staircase,
        dungeon, topstaircase, throne, solar, wardrobe, privy;


    // create the rooms
    gate = new Room("outside the old gate of the castle");
    graveyard = new Room("on a wind-swept gaveyard");
    church = new Room("in a small ancient church");
    throne = new Room("in the throne room with golden walls");// local variable
    // other rooms ...

    // initialise room exits

    gate.setExit("north", graveyard);
    throne.setExit("south", topstaircase);
    // other exits ...
    }

现在,您可以将其设为instance variableclass variable,而不是将其设为private instance variable或{{1}},以便可以访问它,如果您希望将此变量设为{{1}},则提供getter和setter访问此变量的值。