如何从另一个方法访问变量
How to access variables from another method
我在Java有个叫"Game"的class,aim/goal是去某个房间,"throne"房间。当到达王座室时游戏结束。
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 ...
}
}
进入一个 direction/room :
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();
}
}
进入特定房间结束游戏,即 "throne" 房间,我是这样做的:
if (currentRoom.equals(throne)) {
finished = true;
return "Congrats you have reached the throne room";
}
但是我一直收到这个错误:cannot find symbol - variable throne
这是一个基于"Object first with Java using blueJ"
的项目
您可以将其声明为一个字段,而不是在您的 private void createRooms()
中声明 Room 宝座,因此您可以在构造函数之后立即说 Room throne;
,然后您仍然可以在 Room throne;
中对其进行初始化=13=]方法。
你可以考虑制作它final
因为它不会再改变并且清楚地表明了意图
在这种情况下,我不会将它传递给下一个参数,对我来说,将它作为一个字段是有意义的
编辑:关于您无法访问的声明评论的主题,我怀疑您正在做的是在执行以下代码后检查游戏的完成状态:
} else {
currentRoom = nextRoom;
return currentRoom.getLongDescription();
}
因为您已经从此处的方法返回,所以您从未真正检查过游戏的结束状态。
您正在将 Room
存储为 local 变量。一旦您的方法终止,这些变量就会被删除,因此 Room
s 将消失。
要解决这个问题,您应该在 class 级别声明您的变量:
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()
。
But I keep getting this error : "cannot find symbol - variable
throne". I believe i need a field somewhere but I am unsure where and
how this is useful.
您收到此错误是因为变量 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 variable
或 class variable
,这样它就可以访问,而不是将其设为本地,如果您希望将此变量设为 private instance variable
,则提供 getter 和setter 来访问这个变量的值。
我在Java有个叫"Game"的class,aim/goal是去某个房间,"throne"房间。当到达王座室时游戏结束。
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 ...
}
}
进入一个 direction/room :
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();
}
}
进入特定房间结束游戏,即 "throne" 房间,我是这样做的:
if (currentRoom.equals(throne)) {
finished = true;
return "Congrats you have reached the throne room";
}
但是我一直收到这个错误:cannot find symbol - variable throne
这是一个基于"Object first with Java using blueJ"
的项目您可以将其声明为一个字段,而不是在您的 private void createRooms()
中声明 Room 宝座,因此您可以在构造函数之后立即说 Room throne;
,然后您仍然可以在 Room throne;
中对其进行初始化=13=]方法。
你可以考虑制作它final
因为它不会再改变并且清楚地表明了意图
在这种情况下,我不会将它传递给下一个参数,对我来说,将它作为一个字段是有意义的
编辑:关于您无法访问的声明评论的主题,我怀疑您正在做的是在执行以下代码后检查游戏的完成状态:
} else {
currentRoom = nextRoom;
return currentRoom.getLongDescription();
}
因为您已经从此处的方法返回,所以您从未真正检查过游戏的结束状态。
您正在将 Room
存储为 local 变量。一旦您的方法终止,这些变量就会被删除,因此 Room
s 将消失。
要解决这个问题,您应该在 class 级别声明您的变量:
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()
。
But I keep getting this error : "cannot find symbol - variable throne". I believe i need a field somewhere but I am unsure where and how this is useful.
您收到此错误是因为变量 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 variable
或 class variable
,这样它就可以访问,而不是将其设为本地,如果您希望将此变量设为 private instance variable
,则提供 getter 和setter 来访问这个变量的值。