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

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

Setter - 檢查值

Setter - 檢查值

小唯快跑啊 2024-01-28 17:07:32
我應該檢查該值,如果不是該值,我不會更新該值。如果輸入有效,那么我將返回它。最小可重現示例:public class Student {    private int studentId;    private String name;    private double grade;    private double multiplier;    public double getMultiplier() {        return multiplier;    }    /**     * The setter for the multiplier must check that the value is either 1.08 *     * 1.06 or 1.08 or 1.06     *      * If not, then do not update the value     *      * @param multiplier     * @return if the input was valid     */     public boolean setMultiplier(double multiplier) {         if( multiplier == 1.08 * 1.06 || multiplier == 1.08 || multiplier == 1.06 ) { }         return multiplier;    }    ...}
查看完整描述

4 回答

?
斯蒂芬大帝

TA貢獻1827條經驗 獲得超8個贊

public void setMultiplier(double multiplier) { // method head


    if (multiplier == 1.08 * 1.06 || multiplier == 1.08 || multiplier == 1.06) {

        this.multiplier = multiplier; // here the field variable is overwritten

    }


    throw new IllegalArgumentException("exception message");

}

你忘了寫你的類的字段變量。


setter 應該覆蓋字段變量。setter 應該覆蓋字段變量。他沒有返回任何內容,因此在方法頭中返回 void 而不是 boolean 。如果參數錯誤,拋出異常(盡可能有意義)。


提示:我不會將 1.06 或 1.08 等常量分布在代碼中的任何位置。您可以將其定義如下:



public class student {


    private static final double MEANINGFUL_NAME_0 = 1.06;

    private static final double MEANINGFUL_NAME_1 = 1.08;


    // other code below

}


優點:如有必要,您只需在一處更改常量。您錯過并寫 1.07 而不是 1.06 的可能性較低。


查看完整回答
反對 回復 2024-01-28
?
蠱毒傳說

TA貢獻1895條經驗 獲得超3個贊

可能是常量的用例:


private static final double ONE_DOT_ZERO_SIX = 1.06f;

private static final double ONE_DOT_ZERO_EIGHT = 1.08f;


/**

 * Just to know if it is valid

 */

public boolean setMultiplier(double multiplier) {

    return (multiplier == ONE_DOT_ZERO_SIX || multiplier == ONE_DOT_ZERO_EIGHT

            || multiplier == (ONE_DOT_ZERO_SIX * ONE_DOT_ZERO_EIGHT));

}


/**

 * Return the multiplier if it is valid, otherwise 1

 */

public double setMultiplier(double multiplier) {

    if (multiplier == ONE_DOT_ZERO_SIX || multiplier == ONE_DOT_ZERO_EIGHT

            || multiplier == (ONE_DOT_ZERO_SIX * ONE_DOT_ZERO_EIGHT)) {

        return multiplier;

    } else {

        // return any invalid multiplier, this one doesn't change values at least

        return 1;   

    }

}


/**

 * Set the correct multiplier or a substitute

 */

public void setMultiplier(double multiplier) {

    if (multiplier == ONE_DOT_ZERO_SIX || multiplier == ONE_DOT_ZERO_EIGHT

            || multiplier == (ONE_DOT_ZERO_SIX * ONE_DOT_ZERO_EIGHT)) {

        this.multiplier = multiplier;

    } else {

        // return any invalid multiplier, this one doesn't change values at least

        this.multiplier = 1;

    }

}


查看完整回答
反對 回復 2024-01-28
?
當年話下

TA貢獻1890條經驗 獲得超9個贊

Asetter應該更新一個值,并且不返回值(getter的角色),因此它的返回類型應該是void而不是boolean


public void setMultiplier(double multiplier) {

    if( multiplier == 1.08 * 1.06 || multiplier == 1.08 || multiplier == 1.06 ) {

        this.multiplier = multiplier; 

    }

}


查看完整回答
反對 回復 2024-01-28
?
阿晨1998

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

我認為您只是忘記實際設置新值。


因此,如果你這樣做,它應該可以工作:


public void setMultiplier(double multiplier) {



    if( multiplier == 1.08 * 1.06 || multiplier == 1.08 || multiplier == 1.06 ) {

        this.multiplier = multiplier;

    }

}

此外,setter 通常void不是布爾值,并且沒有返回值。


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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