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

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

如何正確引用此代碼中的寶石重量?

如何正確引用此代碼中的寶石重量?

呼啦一陣風 2023-10-19 21:30:11
我如何在這段代碼中引用石頭的重量,以便我可以在我的unlockChest方法中使用它?我基本上試圖將用戶從對象 new Stone()) 輸入的權重相加,因此它 == 組合了用戶在 Chest() 構造函數中輸入的值。我知道這是錯誤的,但我嘗試了 jar1.weight,它說它不是變量。public class Stone{    private String name;    private int weight;    public Stone()    {        System.out.print("Enter stone name: ");        name = Global.keyboard.nextLine();        System.out.print("Enter stone weight: ");        weight = Global.keyboard.nextInt();        Global.keyboard.nextLine();    }}public class Jar{    private int position;    private Stone stone;    public Jar()    {        position = 0;        stone = null;    }    public Jar(int initPos, Stone stone)    {        position = initPos;        this.stone = stone;    }public class Ground{    private Jar jar1;    private Jar jar2;    private Jar jar3;    private Player player;    private Chest chest;    public Ground()    {      player = new Player();      jar1 = new Jar(1, new Stone());      jar2 = new Jar(2, new Stone());      jar3 = new Jar(3, new Stone());      chest = new Chest()    }    public boolean ChestUnlocked()    {        if (jar1.weight + jar2.weight + jar3.weight == chest.combination) //the main problem         return true;        else         return false;    }public class Chest{    public int combination;    private int position;    private Jar jar1;    private Jar jar2;    private Jar jar3;    public Chest()    {        System.out.print("Enter chest combination (5-10): ");        combination = Global.keyboard.nextInt();        Global.keyboard.nextLine();        position = 4;        jar1 = new Jar(4, null);        jar2 = new Jar(4, null);        jar3 = new Jar(4, null);    }
查看完整描述

3 回答

?
炎炎設計

TA貢獻1808條經驗 獲得超4個贊

建議:在每個類中添加 getter/setter,以便您可以使用 getter 和 setter 訪問私有變量。

Stone() 是 Jar() 中的私有對象。假設 Jar() 中有 getStone() 返回: this.stone; 你可以做

if (jar1.getStone().getWeight() + jar2.getStone().getWeight() + jar3.getStone().getWeight() == chest.combination) //the main problem

還要確保在嘗試訪問可為 null 的對象中的方法之前檢查 null 對象(例如,如果 jar1 Stone 為 null,則 jar1.getStone().getWeight() 將失敗)


查看完整回答
反對 回復 2023-10-19
?
蕪湖不蕪

TA貢獻1796條經驗 獲得超7個贊

Stone 可能還有 Stone 類中的權重屬性是私有的。為了正確使用它們,我建議您為您的類實現 getter 方法。實現 getter 后,您可以使用如下方式調用它們:


jar1.getStone().getWeigth()

其中 getStone() 是 Jar 類中的 getter,getWeight() 是 Stone 類中的 getter。


查看完整回答
反對 回復 2023-10-19
?
楊魅力

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

您需要Stone通過添加 getter 來公開權重


public int getWeight() {

    return weight;

}

然后你可以用類似的方法使用它Jar


public int getWeight() {

    if (stone == null) {

        return 0;

    }

    return stone.getWeight();

}

那么你的方法就是


public boolean ChestUnlocked() {

    if (jar1.getWeight() + jar2.getWeight() + jar3.getWeight() == chest.combination) {

         return true;

    }

     return false;

}

假設 Jar 對象和 Chess 對象永遠不為 null


查看完整回答
反對 回復 2023-10-19
  • 3 回答
  • 0 關注
  • 149 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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