亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將項目添加到房間,未定義類型錯誤

將項目添加到房間,未定義類型錯誤

慕妹3146593 2024-01-05 10:21:18
編輯:如果我不清楚(對不起,我是Java的初學者),我只是想添加將項目添加到房間的可能性,但是,我希望項目方面位于其自己的類中,就像目前一樣。盡管在目前的狀態下,它不起作用。我試圖讓物品出現在我的游戲的房間中,所以我制作了一個單獨的物品類,其中相關代碼是public class Items {private String itemDescription;private int itemWeight;/** * Add an item to the Room * @param description The description of the item * @param weight The item's weight */public void addItem(String description, int weight) {    itemDescription = description;    itemWeight = weight;               }/** * Does the room contain an item * @param description the item * @ return the item's weight or 0 if none */public int containsItem(String description) {    if (itemDescription.equals(description))     {        return itemWeight;    }    else    {        return 0;    }}/** * Remove an item from the Room */public String removeItem(String description) {    if (itemDescription.equals(description))     {        String tmp = itemDescription;        itemDescription = null;        return tmp;    }    else     {        System.out.println("This room does not contain" + description);        return null;    }}public String getItemDescription() {    return this.itemDescription;}public void setItemDescription(String itemDescription) {    this.itemDescription = itemDescription;}public int getItemWeight() {    return this.itemWeight;}public void setItemWeight(int itemWeight) {    this.itemWeight = itemWeight;}public String getCharacter() {    return this.character;}public void setCharacter(String character) {    this.character = character;}}在我單獨的游戲類中,我嘗試將其鏈接到我的游戲類中,就像這樣     // initialise room exits        outside.setExit("north", lab);        outside.addItem("notebook", 2); 就像我對游戲出口所做的那樣。但是,我收到一條錯誤消息The method addItem(String, int) is undefined for the type Room我認為這是因為,名為“Room”的類沒有任何對“item”的引用,但是我不確定如何實現這一目標?任何指導將不勝感激。
查看完整描述

2 回答

?
30秒到達戰場

TA貢獻1828條經驗 獲得超6個贊

The method addItem(String, int) is undefined for the type 

Room

因為您的房間類中沒有 addItem(String, Item) 方法定義。在嘗試調用之前,將此函數添加到 Room 類中?;蛘呷缦聢D修復


如果沒有它,您將收到編譯錯誤。


看到你的評論,讓我給你一些建議,但要小心,因為我不知道你的確切需求。


import java.util.HashMap;

import java.util.Set;

import java.util.ArrayList;


public class Room 

{


        public Items item = new Items();

       ...........

在調用類時使用它就像


         outside.item.addIem(...)


查看完整回答
反對 回復 2024-01-05
?
叮當貓咪

TA貢獻1776條經驗 獲得超12個贊

您可以在此處將項目邏輯與房間邏輯混合。

  1. 你的addItem函數應該是 Item 的構造函數

  2. 您應該在 Room 類中具有 addItem 函數,該函數實例化一個新項目(如果您想在房間中放置超過 1 個項目,則可能將其存儲在列表/哈希中)

  3. containsItem方法也應該位于 Room 類中


查看完整回答
反對 回復 2024-01-05
  • 2 回答
  • 0 關注
  • 158 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號