這個問題是一個跟進另一個問題:Abstract class with default value數學運算符和Java數字
我試圖定義一個抽象的Range類將作爲基實現數字範圍類的。使用目的是無關緊要的這個問題,但到目前爲止,我有:
/**
* Abstract generic utility class for handling ranges
*/
public abstract class Range<T extends Number> implements Collection<T>{
// Variables to hold the range configuration
protected T start;
protected T stop;
protected T step;
/**
* Constructs a range by defining it's limits and step size.
*
* @param start The beginning of the range.
* @param stop The end of the range.
* @param step The stepping
*/
public Range(T start, T stop, T step) {
this.start = start;
this.stop = stop;
this.step = step;
}
}
(無關的東西上面略)
現在我想要實現Collection
,所以我實現Size
在我的抽象類如下:
@Override
public int size() {
return (this.stop - this.start)/this.step;
}
但Number
似乎是一般的工作。我是否需要在子類中實現它,還是有一種抽象的方式?
另請參閱:http://stackoverflow.com/questions/25252441/if-any-object-of-the-java-abstract-class-number-is-equal-to-zero – assylias 2014-08-27 11:58:07