如何使用模板创建一个对两个数字执行操作的类

时间:2015-03-09 10:42:18

标签: java templates generics types operators

我想创建一个名为“BasicInterval”的泛型类,它包含两个私有对象start_和stop_,它们将被限制为Number和Comparable: 我的目标是添加将返回start>的公共函数。停止?,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_;
}

我将通用类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); }
    }
}

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

感谢。

1 个答案:

答案 0 :(得分:0)

您必须为BasicInterval课程使用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 BasicInterval<T, S extends Number2<T>>

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

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

另外,正如您所注意到的,您还需要将要使用的Number2实例传递给BasicInterval构造函数。所以,基本上你就是Number2个实例 - num,操作超过2个Number个实例 - start_stop_