Java泛型,从文字强制类型

时间:2015-03-29 12:51:12

标签: java generics types

如何在底部的one()和zero()方法中强制使用以下代码中的文字类型? Eclipse将允许它进行编译并运行但Eclipse会发出警告(Fraction是一种原始类型。对泛型类型Fraction的引用应该参数化)并且它似乎不是类型安全的。我意识到Fraction对象绑定到E所代表的任何类型,但事实是我传递整数来创建一个可能是双重关注的新分数。有没有办法传递E类型的文字值?

/**
 * A class representing fraction.
 * @param <A> generic type
 */
public class Fraction<A extends Number> extends Number
{
    /**
     * Constructs a new Fraction.
     * @param n numerator
     * @param d denominator
     * @precondition n != null && d != null && d != Double(0.0)
     */
    public Fraction(A n, A d)
    {
        assert n != null && d != null : "arguments cannot be null";
        assert (double) d != 0.0 : "denominator must not be zero";

        num = n;
        denom = d;
    }

    /** the numerator. */
    private A num;

    /** the denominator. */
    private A denom;

    /** serial version UID for serialization. */
    private static final long serialVersionUID = 161146059432882814L;
}



public class FractionOperation<E extends Number> implements Arithmetic<Fraction<E>>
{
    /**
     * Class constructor generates a new instance. The operations variable type
     * must be compatible with the type declared for FractionOperation<E>.
     * @param operations any object that implements IntegerArithmetic<E>
     */
    public FractionOperation(IntegerArithmetic<E> operations)
    {
        this.op = operations;
    }

    /**
     * Computes the greatest common divisor for a and b.
     * @param a an number
     * @param b an number
     * @return greatest common divisor of a and b
     */
    private E gcd(E a, E b)
    {
        if (0.0 == b.doubleValue())
            return a;

        return gcd(b, op.mod(a, b));
    }

    /**
     * Simplifies the fraction num/denom by dividing both by their greatest
     * common divisor.
     * @param num numerator
     * @param denom denominator
     * @return new fraction (num / gcd(num, denom)) / (denom / gcd(num, denom))
     */
    private Fraction<E> simplify(E num, E denom) {
        E gcd = gcd(num, denom);
        E nomSimpl = op.div(num, gcd);
        E denomSimpl = op.div(denom, gcd);

        return new Fraction<E>(nomSimpl, denomSimpl);
    }

    /**
     * Adds two fraction and simplifies the result.
     * @param lhs left hand side argument
     * @param rhs right hand side argument
     * @return lhs+rhs (simplified)
     */
    public Fraction<E> add(Fraction<E> lhs, Fraction<E> rhs) {
        E nom = op.add
            (
                op.mul(lhs.getNumerator(), rhs.getDenominator()),
                op.mul(rhs.getNumerator(), lhs.getDenominator())
            );
        E denom = op.mul(lhs.getDenominator(), rhs.getDenominator());

        return simplify(nom, denom);
    }
    /**
     * returns a representation for 0.
     * @return 0
     */
    public Fraction zero() {

        return new Fraction(0, 1);
    }

    /**
     * returns a representation for 1.
     * @return 1
     */
    public Fraction one() {
        return new Fraction(1, 1);
    }

    public E mod(E numerator, E denominator)
    {
        return op.mod(numerator, denominator);
    }

    private IntegerArithmetic<E> op;
}

我通过使用op方法one()和zero()代替泛型类型的文字来修复此代码中的问题。我仍然想知道如何强制使用文字以供将来参考。

/**
 * returns a representation for 0.
 * @return 0
 */
public Fraction<E> zero() {

    return new Fraction<E>(op.zero(), op.one());
}

/**
 * returns a representation for 1.
 * @return 1
 */
public Fraction<E> one() {
    return new Fraction<E>(op.one(), op.one());
}

2 个答案:

答案 0 :(得分:0)

  

返回新分数(1,1);

在Java 7中,您可能需要

new Fraction<>(1, 1)

并且在Java 7之下,你根本无法强制推断。

你应该发布你的Eclipse警告,而不仅仅是说它们发生了。

答案 1 :(得分:0)

one()zero()方法的问题是它们返回raw类型。此外,它们应该是static(即工厂)方法。

将其更改为:

public static Fraction<Integer> zero() {
    return new Fraction<Integer>(0, 1);
}

public static Fraction<Integrr> one() {
    return new Fraction<Integer>(1, 1);
}