数字操作的通用类 [英] Generic class for operations on Number

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

问题描述

我正在写一个Java类来表示一个范围。我想使它更通用,以便范围可以是任何数字类型,但是两个值v1和v2必须使用相同的数字类型。

尽管我可以对它们进行比较,但我无法添加或减去这些值。我知道对两种不同数字类型的操作会导致问题(例如Float + Integer),Number的子类也是如此。 BigInteger和BigDecimal。但在我的情况下,v1和v2应该是相同的类型。

  class Range< T extends Number&可比< T>> {
public T v1;
public T v2;
public Range(T v1,T v2){
if(v1.compareTo(v2)> 0)
throw new IllegalArgumentException(Value-1 must be smaller or equal to Value- 2.);
this.v1 = v1;
this.v2 = v2;
}
public T length(){
return v2 - v1; //编译错误
}
}

最近我只想出了一个这样的想法:

  public T add(T t1,T t2){
if(t1 instanceof Integer){
// ...
}
if(t1 instanceof Long){
// ...
}
}
code>

但是有没有更好的设计来创建Range类?
Thanks!

解决方案

使范围 length() abstract。另请注意,应保护 v1 v2

 抽象类Range< T extends Number&可比< T>> {
protected T v1;
保护T v2;
public Range(T v1,T v2){
if(v1.compareTo(v2)> 0)
throw new IllegalArgumentException(Value-1 must be smaller or equal to Value- 2.);
this.v1 = v1;
this.v2 = v2;
}

public abstract T length();
}

扩展范围类为特定类型:

 类IntegerRange扩展范围<整数> {
@Override
public Integer length(){
return v2 - v1;




$ b

这符合开放式关闭原则。一个班级应该开放延期,但因修改而关闭。使用 instanceof 检查是违背此原则的最简单方法,因为 Range 需要为每种新类型修改必须支持。这应该避免在你的情况。


I am writing a Java class to represent a range. I would like to make it more general so that the range can be any Number type, but the two values v1 and v2, must be in the same Number type.

Although I can do comparison on them, I cannot add or subtract the values. I know the operation on two different number types cause problem (e.g. Float + Integer), and so do subclasses of Number, e.g. BigInteger and BigDecimal. But in my case, v1 and v2 should be of the same type.

class Range<T extends Number & Comparable<T>> {
    public T v1;
    public T v2;
    public Range(T v1, T v2) {
        if (v1.compareTo(v2) > 0)
            throw new IllegalArgumentException("Value-1 must be smaller or equal to Value-2.");
        this.v1 = v1;
        this.v2 = v2;
    }
    public T length() {
        return v2 - v1; // Compilation error
    }
}

Recently I only come up with an idea like this:

public T add(T t1, T t2) {
    if (t1 instanceof Integer) {
        // ...
    }
    if (t1 instanceof Long) {
        // ...
    }
} 

But is there any better design to create the Range class? Thanks!

解决方案

Make Range and length() abstract. Also note that v1 and v2 should be protected.

abstract class Range<T extends Number & Comparable<T>> {
    protected T v1;
    protected T v2;
    public Range(T v1, T v2) {
        if (v1.compareTo(v2) > 0)
            throw new IllegalArgumentException("Value-1 must be smaller or equal to Value-2.");
        this.v1 = v1;
        this.v2 = v2;
    }

    public abstract T length(); 
}

extend the range class for specific types :

class IntegerRange extends Range<Integer> {
     @Override
      public Integer length() {
          return v2 - v1;
      }
 }

This is in line with the open-closed principle. A class should be open for extension, but closed for modification. Using instanceof checks is the easiest way to go against this principle since Range will need to modified for each new type that it has to support. This should be avoided in your case.

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

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