用通用数字算术 [英] Arithmetic with generic Numbers

查看:102
本文介绍了用通用数字算术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我必须接受一个泛型类型T,它将是一个数字基元的包装类,并对它进行一些算术运算。我认为这会因为自动装箱和取消装箱而变得微不足道:

  T newval = val1 * val2; 

(其中val1和val2已经是T类型的变量,其中有适当的值。)



但事实证明,如果Java在编译时不明确地知道类型,它将不会取消装箱值。我还认为我可以使用乘法方法,使得每个Number子类(我打算使用)都具有它们。所以我宣布我的课程是:

  public class SomeClass< T extends Number> {
// ...
}

然后:

  T newval = val1.multiiply(val2); 

但这不起作用,因为我猜并非Number的所有子类都是必需的他们为此使用了一个接口?)拥有该方法(但我想使用DO的所有方法)。



我已经看到一些类似的问题,但他们所有这些都导致OP被告知不使用泛型,但是我必须在我正在执行的工作中使用泛型,否则整个事情都毫无价值。任何人都可以想到一个适合我的解决方案吗?



编辑:
好​​吧,他回到我身边并澄清了什么意思是说,这是不同的,我的问题不再与我在做什么有关。谢谢你们。

解决方案

各种算术运算符只能应用于可转换为基本类型的类型的值。没有办法用泛型表达这一要求。因此,泛型不适合执行此任务。



对于泛型方法,改为为数字基元类型提供重载。
对于泛型类,提供使用特定数字类型的子类。

 类NumericCalculator< T> {
public void calculate(T one,T two){
T result = one * two; //编译器错误
}
}

class LongCalculator扩展了NumericCalculator< Long> {
public void calculate(Long one,Long two){
Long result = one * two; //是
}
}

使用操作符的实现将会在知道实际类型的子类中。


So I have to accept a generic type T, which will be a wrapper class for one of the numeric primitives, and do some arithmetic with it. I thought this would be trivial because of autoboxing and unboxing:

T newval = val1*val2;

(Where val1 and val2 are already variables of type T with appropriate values in them.)

But it turns out that Java won't unbox values if it doesn't know the type explicitly at compile time. I also thought that I could just use the multiply method that each of the Number subclasses (that I intend to use) have within them. So I declared my class with:

public class SomeClass<T extends Number> {
    // ...
}

and then:

T newval = val1.multiiply(val2);

But that doesn't work because I guess not all subclass' of Number are required (Why don't they use an interface for this?) to have that method (yet all the ones that I want to use DO.)

I've seen some similar questions but they all resulted in OP being told to just not use generics, yet I am required to use generics for the work I'm doing, otherwise the entire thing is worthless. Can anybody think of a solution that will work for me?

EDIT: Okay so he got back to me and clarified what was meant, it's different enough that my question is no longer relevant to what i'm doing. Thanks guys.

解决方案

The various arithmetic operators can only be applied to values of types that are convertible to primitive types. There is no way to express that requirement with generics. Therefore generics are not suited for this task.

For generics methods, instead provide overloads for the numeric primitive types. For generic classes, provide subclasses that use a specific numeric type.

class NumericCalculator<T> {
    public void calculate(T one, T two) {
        T result = one * two; // compiler error
    }
}

class LongCalculator extends NumericCalculator<Long> {
    public void calculate(Long one, Long two) {
        Long result = one * two; // yes
    }
}

The implementation that uses the operators will have to be in the subclasses which know the actual type.

这篇关于用通用数字算术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆