第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

將項(xiàng)目添加到房間,未定義類型錯(cuò)誤

將項(xiàng)目添加到房間,未定義類型錯(cuò)誤

慕妹3146593 2024-01-05 10:21:18
編輯:如果我不清楚(對不起,我是Java的初學(xué)者),我只是想添加將項(xiàng)目添加到房間的可能性,但是,我希望項(xiàng)目方面位于其自己的類中,就像目前一樣。盡管在目前的狀態(tài)下,它不起作用。我試圖讓物品出現(xiàn)在我的游戲的房間中,所以我制作了一個(gè)單獨(dú)的物品類,其中相關(guān)代碼是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;}}在我單獨(dú)的游戲類中,我嘗試將其鏈接到我的游戲類中,就像這樣     // initialise room exits        outside.setExit("north", lab);        outside.addItem("notebook", 2); 就像我對游戲出口所做的那樣。但是,我收到一條錯(cuò)誤消息The method addItem(String, int) is undefined for the type Room我認(rèn)為這是因?yàn)?,名為“Room”的類沒有任何對“item”的引用,但是我不確定如何實(shí)現(xiàn)這一目標(biāo)?任何指導(dǎo)將不勝感激。
查看完整描述

2 回答

?
30秒到達(dá)戰(zhàn)場

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超6個(gè)贊

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

Room

因?yàn)槟姆块g類中沒有 addItem(String, Item) 方法定義。在嘗試調(diào)用之前,將此函數(shù)添加到 Room 類中?;蛘呷缦聢D修復(fù)


如果沒有它,您將收到編譯錯(cuò)誤。


看到你的評論,讓我給你一些建議,但要小心,因?yàn)槲也恢滥愕拇_切需求。


import java.util.HashMap;

import java.util.Set;

import java.util.ArrayList;


public class Room 

{


        public Items item = new Items();

       ...........

在調(diào)用類時(shí)使用它就像


         outside.item.addIem(...)


查看完整回答
反對 回復(fù) 2024-01-05
?
叮當(dāng)貓咪

TA貢獻(xiàn)1776條經(jīng)驗(yàn) 獲得超12個(gè)贊

您可以在此處將項(xiàng)目邏輯與房間邏輯混合。

  1. 你的addItem函數(shù)應(yīng)該是 Item 的構(gòu)造函數(shù)

  2. 您應(yīng)該在 Room 類中具有 addItem 函數(shù),該函數(shù)實(shí)例化一個(gè)新項(xiàng)目(如果您想在房間中放置超過 1 個(gè)項(xiàng)目,則可能將其存儲(chǔ)在列表/哈希中)

  3. containsItem方法也應(yīng)該位于 Room 類中


查看完整回答
反對 回復(fù) 2024-01-05
  • 2 回答
  • 0 關(guān)注
  • 154 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號