如何使用对两个数字进行运算的模板创建 class

How do I create a class with template that does operations on two numbers

我想创建一个名为 "BasicInterval" 的通用 class,它包含两个私有对象 start_ 和 stop_,它们仅限于 Number 和 Comparable: 我的目标是添加 public 函数 return start > stop ?, start_ - stop_, ...

public class BasicInterval<T extends Number2> {
    public BasicInterval( T start, T stop ) {
        this.start_ = start;
        this.stop_ = stop;
    }

    public boolean isRev() {
        return start_.compareTo(start_,stop_) < 1;
    }

    public T width() {
        return start_.subtract(start_, stop_);
    }

    public T start_;
    public T stop_;
}

我将通用 class Number 扩展为 Number2:

public class Number2<T extends Number> {
    private final Calculator<T> calc_;
    public Number2(Calculator<T> calc) {
        this.calc_ = calc;
    }

    public T add(T a,T b) { return calc_.add(a,b); }
    public T subtract(T a,T b) { return calc_.subtract(a,b); }
    public T multiply(T a,T b) { return calc_.multiply(a,b); }
    public T divide(T a,T b) { return calc_.divide(a,b); }
    public Integer compareTo(T a,T b) { return calc_.compareTo(a,b); }

    public interface Calculator<T extends Number> {
        public T add(T a, T b);
        public T subtract(T a, T b);
        public T multiply(T a, T b);
        public T divide(T a, T b);
        public Integer compareTo(T a, T b);
    }

    public static class DoubleCalc implements Calculator<Double> {
        public final static DoubleCalc INSTANCE = new DoubleCalc();
        private DoubleCalc(){}
        public Double add(Double a,Double b) { return a + b; }
        public Double subtract(Double a,Double b) { return a - b; }
        public Double multiply(Double a,Double b) { return a * b; }
        public Double divide(Double a,Double b) { return a / b; }
        public Integer compareTo(Double a,Double b) { return a.compareTo(b); }
    }

    public static class FloatCalc implements Calculator<Float> {
        public final static FloatCalc INSTANCE = new FloatCalc();
        private FloatCalc(){}
        public Float add(Float a,Float b) { return a + b; }
        public Float subtract(Float a,Float b) { return a - b; }
        public Float multiply(Float a,Float b) { return a * b; }
        public Float divide(Float a,Float b) { return a / b; }
        public Integer compareTo(Float a,Float b) { return a.compareTo(b); }
    }

    public static class IntCalc implements Calculator<Integer> {
        public final static IntCalc INSTANCE = new IntCalc();
        private IntCalc(){}
        public Integer add(Integer a,Integer b) { return a + b; }
        public Integer subtract(Integer a,Integer b) { return a - b; }
        public Integer multiply(Integer a,Integer b) { return a * b; }
        public Integer divide(Integer a,Integer b) { return a / b; }
        public Integer compareTo(Integer a,Integer b) { return a.compareTo(b); }
    }
}

class Number2 可以编译,但我无法在 BasicInterval 中使用 Number2。有人可以就此事向我提出建议吗?

谢谢。

您必须为 BasicInterval class 使用 2 种类型的参数。一种类型参数将具有 Number 上限,并将该类型参数用于 Number2 类型参数。

class BasicInterval<T extends Number, S extends Number2<T>> {
    public BasicInterval(S num, T start, T stop) {
        this.num = num;
        this.start_ = start;
        this.stop_ = stop;
    }

    public boolean isRev() {
        return num.compareTo(start_, stop_) < 1;
    }

    public T width() {
        return num.subtract(start_, stop_);
    }

    public S num;
    public T start_;
    public T stop_;
}

T extends Number2 不起作用,因为 Number2 是原始类型。您需要提供类型参数。所以,你可以做到:S extends Number2<T>。但是 T 应该首先定义,即使用第一个类型参数的地方。这使您的 class 为:

class BasicInterval<T, S extends Number2<T>>

但是,Number2<T> 将不起作用,因为根据 class 声明,T 的上限应该是 Number。因此,继续前进,并绑定 T:

class BasicInterval<T extends Number, S extends Number2<T>>

此外,如您所见,您还需要将要使用的 Number2 实例传递给 BasicInterval 构造函数。所以,基本上你有 Number2 个实例 - num,运行 2 个 Number 个实例 - start_stop_.