繁体   English   中英

在Java中,是否可以将数字开箱到适当的类型?

[英]In Java, is it possible to unbox an Number to an appropiate type?

class Node<T extends Number> implements Comparable<Node<T>>{
private T data;
private Node<T> next;
private Node<T> prev;

public Node(T data){
    this.data = data;
    next = null;
    prev = null;
}

public int compareTo(Node<T> o){
    if(o.data > this.data){
        return -1;
    }else if(o.data < this.data){
        return 1;
    }else{
        return 0;
    }
}

我写了上面的代码,当我尝试编译它时,我得到了

    Queue.java:14: error: bad operand types for binary operator '>'
            if(o.data > this.data){
                      ^
  first type:  T
  second type: T
  where T is a type-variable:
    T extends Number declared in class Queue.Node

    Queue.java:16: error: bad operand types for binary operator '<'
            }else if(o.data < this.data){
                            ^
  first type:  T
  second type: T
  where T is a type-variable:
    T extends Number declared in class Queue.Node
    2 errors

只是为了您不会被Queue.node弄糊涂,Class Node嵌入在Class Queue中

因此,我猜测Java不会自动取消对Number的装箱,但是有办法吗?

谢谢

您可以使用doubleValue()

class Node<T extends Number> implements Comparable<Node<T>>
{
    private T data;
    private Node<T> next;
    private Node<T> prev;

    public Node(T data)
    {
        this.data = data;
        next = null;
        prev = null;
    }

    public int compareTo(Node<T> o)
    {
        if(o.data.doubleValue() > this.data.doubleValue()) return -1;
        else if(o.data.doubleValue() < this.data.doubleValue()) return 1;
        else return 0;
    }
}

Number类是抽象的,不能与扩展它的类一样使用。

例如:

// this doesnt work
Number a = 22;
Number b = 33;
Number c = a - b; // compile error
if(a > b) // compile error

您需要比较这些Number对象的一些值。

// this works
Number a = 22;
Number b = 33;
Number c = a.doubleValue() - b.doubleValue();
if(a.longValue() > b.longValue())

因此,要修复您的代码,您的语句应读取if(o.data.doubleValue() > this.data.longValue())

所以我猜Java不会自动取消装箱

那是对的。 仅当您尝试使用的静态表达式类型是原始包装类型之一时,才可以进行拆箱。 例如

    Number n = ...
    int i = 1 + n;               // Compilation error
    int j = 1 + ((Integer) n);   // OK ... provided the cast is going to work.

但是有办法吗?

如果您知道Number是特定类型,则可以按上述方法进行转换。 否则,您可以显式调用各自的...Value()方法。

    int j = 1 + n.intValue();

但是,您无法使运算符(“ +”,“>”等)表现得好像对Number类而言已重载。

不要理会T作为Number :使T extend Comparable<T> ,并将其实现为return this.data.compareTo(o.data)

您可以在节点Type上使用intValue()方法,该方法将返回整数值。 其他数据类型(doubleValue(),longValue()等)也可以使用类似的方法。

以下是代码片段的样子:

public int compareTo(Node<T> o){
   if(o.data.intValue() > this.data.intValue()){
      return -1;
    }else if(o.data.intValue() < this.data.intValue()){
      return 1;
    }else{
       return 0;
    }
}

如果要使用longdouble ,则应使用intValue()或方法(或其任何其他版本),如果不是Integer则也可以使用。

public int compareTo(Node<T> o){
    if(o.data.intValue() > this.data.intValue()){
        return -1;
    }else if(o.data.intValue() < this.data.intValue()){
        return 1;
    }else{
        return 0;
    }
} 

虽然通常您无法提出要求,但是在这种情况下,您可以使用Number的所有子类也实现Comparable的事实。 如果在T的定义中添加&可比,则可以简单地比较这些值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM