我有个问题。我正在创建数字接口,我正在接口中创建静态最终对象。如果我修改对象并检查相等性,则返回 true。我覆盖了“等于”,问题没有改变。
这是代码:
public interface Numeric<T extends Number> extends Cloneable, Comparable<T> {
public static final Numeric<Short> SHORT = new Numeric<Short>() {
private short value = (short) 0;
@override
public Numeric<Short> add(Short value) {
this.value += value;
return this;
}
@override
public Short value() {
return value;
}
}
public Numeric<T> add(T value);
public T value();
//....
}
public class Test {
public static void main(String[] args) {
Numeric<Short> one = Numeric.SHORT;
Numeric<Short> two = Numeric.SHORT;
one.add(Short.MIN_VALUE);
System.out.println(two.value()); //print -32768!!!!! why not 0?
}
}
谢谢解答!