Setter - 检查值

Setter - Checking the value

我应该检查该值,如果不是该值,我不会更新该值。如果输入有效,那么我 return 它。

最小可重现示例:

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;
    }
    ...
}

A​​ setter 应该更新一个值,而不是 return 一个值(getter 的作用)所以它的 return 类型应该是 void 而不是 boolean

public void setMultiplier(double multiplier) {
    if( multiplier == 1.08 * 1.06 || multiplier == 1.08 || multiplier == 1.06 ) {
        this.multiplier = multiplier; 
    }
}
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");
}

您忘记写入class的字段变量。

A​​ setter 应该覆盖字段变量。 setter 应该覆盖字段变量。他没有返回任何东西,因此方法头中的 void 而不是布尔值。如果参数错误,则抛出异常(尽可能有意义)。

提示:我不会在代码中的任何地方分配我的常量,如 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 的可能性较低。

我想你只是忘了实际设置新值。

因此,如果您这样做,它应该可以工作:

public void setMultiplier(double multiplier) {


    if( multiplier == 1.08 * 1.06 || multiplier == 1.08 || multiplier == 1.06 ) {
        this.multiplier = multiplier;
    }
}

此外,setter 通常是 void 而不是布尔值,并且没有 return 值。

可能是常量的用例:

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;
    }
}