为了账号安全,请及时绑定邮箱和手机立即绑定

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 关注
  • 41 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信