「Range」クラスを (Java で) 実装して、ラップする int 値に境界強制機能を提供しようとしています。私は、そのサブクラスのそれぞれが、それらの境界を強制するロジックを書き直すことなく、独自の最小/最大境界を定義することを望んでいます。次に例を示します。
public abstract class Range {
// I would like each derived class to possess its own distinct instances of the
// min/max member data
protected static final int MIN_VAL;
protected static final int MAX_VAL;
protected int _value;
public void set (int newVal) {
// Range check the input parameter
// this should use the min/max bounds for the object's most derived class
if (newVal < MIN_VAL || newVal > MAX_VAL) {
throw new InvalidParameterException("`newVal` is out of range");
}
this._value = newVal;
}
public int get() {
return this._value;
}
}
// This class should limit its wrapped value to values between 1 and 6 inclusively
public class Die extends Range {
public Die() {
MIN_VAL = 1;
MAX_VAL = 6;
this.set (1);
}
}
明らかに、この実装は機能しませんが、どうすれば目標を達成できますか? ロジックの多くを繰り返さずにこれは可能ですか?