我目前正在努力改善" Zuul世界"使用BlueJ。我正在进行Item
课程,并在那里放了几个项目。下一步是将项目放入房间,我认为这是我面临的当前问题:您如何引用或使用在另一个类中建立的ArrayList
?希望如果我能理解这是怎么做的,那么我可以研究如何在特定的房间里放置一个或多个特定物品。
任何帮助都将不胜感激。
请参阅下面的代码:
Item
类
public Item(String itemDescription, int itemWeight)
{
// initialise instance variables
itemDescription = itemDescription;
itemWeight = itemWeight;
this.list = new ArrayList<Item>();
Item item = new Item(itemDescription, itemWeight);
}
/**
* Name of item
*/
public String getItemDescription()
{
return itemDescription;
}
/**
* Weight of an Item
*/
public int itemWeight()
{
return itemWeight;
}
/**
* Show items
*/
public ArrayList<Item> getItems ()
{
return list;
}
public String getItemString()
{
String returnString = "Item:";
{
returnString += ""+list;
}
return returnString;
}
/**
* Presents name and weight of item
*/
public String toString()
{
return "Item: " + itemDescription + "Weight " + itemWeight;
}
/**
* List of items
*/
public void addItem()
{
list.add(new Item("can of coke",1));
list.add(new Item("bike",6));
list.add(new Item("textbook",4));
list.add(new Item("£20 note",1));
list.add(new Item("stick",2));
list.add(new Item("Theater leaflet",1));
list.add(new Item("mobile phone",2));
}
Room
课程
public class Room
{
private String description;
private HashMap<String, Room> exits;
public Room(String description)
{
this.description = description;
}
/**
* Define the exits of this room. Every direction either leads
* to another room or is null (no exit there).
* @param north The north exit.
* @param east The east east.
* @param south The south exit.
* @param west The west exit.
*/
public void setExit(String direction, Room neighbor)
{
exits.put(direction, neighbor);
}
public Room getExit(String direction)
{
return exits.get(direction);
}
/**
* @return The description of the room.
*/
public String getDescription()
{
return description;
}
/**
* Return a long description of this room, of the form;
* You are in the kitchen.
* Exits: north west
* @return A description of the room, including exits.
*/
public String getLongDescription()
{
return "You are" + description + "./n" +getExitString();
}
/**
* Return a description of the room's exits,
* for example, "Exits: north west".
* @return A description of the available exits.
*
*/
public String getExitString()
{
String returnString = "Exits:";
Set<String> keys = exits.keySet();
for(String exit :keys) {
returnString += "" + exits;
}
return returnString;
}
}