Java - 从其他类调用对象? [重复]

时间:2022-07-06 11:58:04

This question already has an answer here:

这个问题在这里已有答案:

I'm making a DnD program to essentially just keep track of everything. I have three classes relevant to this problem, my main (which is just a hub to execute everything else), an Item class which holds all the properties of various individual items, and an InventoryList class which basically just defines an unholy load of items(this is a separate class because of the sheer number of items in 4e DnD). I had decided on making an Array of these Items, and calling that the users inventory; first off, is there a better way to do that -- i.e. using a HashMap or ArrayList? Second, I'm getting a classic "error, cannot find symbol" compiler error in the following code (shortened to relevance):

我正在制作一个DnD程序,基本上只是跟踪所有内容。我有三个与此问题相关的类,我的主要(它只是执行其他所有内容的集线器),一个Item类,它包含各个单独项的所有属性,以及一个InventoryList类,它基本上只定义了一个unholy负载的项目(这是一个单独的类,因为4e DnD中的项目数量很多)。我已经决定制作这些项目的数组,并称之为用户库存;首先,是否有更好的方法 - 即使用HashMap或ArrayList?其次,我在以下代码中获得了经典的“错误,找不到符号”编译器错误(缩写为相关性):

Main Method:

public class DnD {
    public static void main (String[] args) throws FileNotFoundException, IOException {
        . . .
        Item[] myInventory = {Candle}; //error: symbol cannot be found
    } //main()
} //DnD

Item Method:

public class Item {
    . . .
    public static Item Candle = new Item("Candle", 1, "Provides dim light for 2 squares, and lasts for 1 hour.");
    public Item(String _name, double _price, String _desc) {
        name = _name;
        price = priceConverter(_price);
        description = _desc;
    } //Item()
} //Item

InventoryList Method:

public class InventoryList {
    . . .
    public InventoryList() {
        // I have no idea where to proceed with this.
        // Is there a way for me to use this to initialize ...
        // ... an Array of Items in this method, to use in the main?
        // The Item object stores name, price, and a description; ...
        // ... is there a way to create an Array or similar to ...
        // ... display all that information at once and hold it together?
    }
}

I seem to be unable to summon Items in the main from the Item class. My Inventory (Item[]) is not working because of this.

我似乎无法从Item类中召唤主要的项目。我的库存(Item [])因此而无效。

2 个解决方案

#1


1  

For the "cannot find symbol" error, you can fix it like this:

对于“无法找到符号”错误,您可以像这样修复它:

Item[] myInventory = {Item.Candle};

Candle is defined in the class Item, so you have to type the class name out as well.

Candle在类Item中定义,因此您还必须输出类名。

For how to implement InventoryList, I recommend using an ArrayList<Item> to store the player's items:

有关如何实现InventoryList,我建议使用ArrayList 来存储播放器的项目:

private ArrayList<Item> items = new ArrayList<>();

public ArrayList<Item> getItems() {
    return items;
}

ArrayLists can dynamically change size, so if you want to add more items to the player's inventory, you can do that very conveniently.

ArrayLists可以动态地改变大小,因此如果你想在玩家的库存中添加更多项目,你可以非常方便地做到这一点。

#2


0  

You must qualify the static property

您必须限定静态属性

Item[] myInventory = {Item.Candle};

Second question is rather braod, do more research and ask a more focussed question.

第二个问题是相当的问题,做更多的研究,并提出一个更集中的问题。

#1


1  

For the "cannot find symbol" error, you can fix it like this:

对于“无法找到符号”错误,您可以像这样修复它:

Item[] myInventory = {Item.Candle};

Candle is defined in the class Item, so you have to type the class name out as well.

Candle在类Item中定义,因此您还必须输出类名。

For how to implement InventoryList, I recommend using an ArrayList<Item> to store the player's items:

有关如何实现InventoryList,我建议使用ArrayList 来存储播放器的项目:

private ArrayList<Item> items = new ArrayList<>();

public ArrayList<Item> getItems() {
    return items;
}

ArrayLists can dynamically change size, so if you want to add more items to the player's inventory, you can do that very conveniently.

ArrayLists可以动态地改变大小,因此如果你想在玩家的库存中添加更多项目,你可以非常方便地做到这一点。

#2


0  

You must qualify the static property

您必须限定静态属性

Item[] myInventory = {Item.Candle};

Second question is rather braod, do more research and ask a more focussed question.

第二个问题是相当的问题,做更多的研究,并提出一个更集中的问题。