[ACCEPTED]-Abstract class with default value-default-value
Accepted answer
One solution would be to ask the subclasses 1 for the default step to use:
public abstract class Range<T extends Number> {
private T start;
private T stop;
private T step;
public Range(T start, T stop, T step) {
this.start = start;
this.stop = stop;
this.step = step;
}
protected Range(T start, T stop) {
this.start = start;
this.stop = stop;
this.step = getDefaultStep();
}
protected abstract T getDefaultStep();
}
public class IntegerRange extends Range<Integer> {
public IntegerRange(Integer start, Integer stop, Integer step) {
super(start, stop, step);
}
public IntegerRange(Integer start, Integer stop) {
super(start, stop);
}
@Override
protected Integer getDefaultStep() {
return 1;
}
}
public class DoubleRange extends Range<Double> {
public DoubleRange(Double start, Double stop, Double step) {
super(start, stop, step);
}
public DoubleRange(Double start, Double stop) {
super(start, stop);
}
@Override
protected Double getDefaultStep() {
return 1d;
}
}
You can use this implementation:
protected Range(T start, T stop) {
this(start, stop, (T) (new Integer(1)));
}
0
You will run into another problems ... but 1 here is your number 1:
Number ONE = new Number() {
@Override
public int intValue() {
return 1;
}
@Override
public long longValue() {
return 1L;
}
@Override
public float floatValue() {
return 1.0f;
}
@Override
public double doubleValue() {
return 1.0;
}
@Override
public byte byteValue() {
return 1;
}
@Override
public short shortValue() {
return 1;
}
};
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.