简体   繁体   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;
    }
}

I wrote the above code and when I try to compile it I get 我写了上面的代码,当我尝试编译它时,我得到了

    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

just so you don't get confused by the Queue.node, Class Node is embedded in Class Queue 只是为了您不会被Queue.node弄糊涂,Class Node嵌入在Class Queue中

So I'm guessing that Java does not auto unbox Number but is there a way to do it? 因此,我猜测Java不会自动取消对Number的装箱,但是有办法吗?

Thanks 谢谢

You can use doubleValue() : 您可以使用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;
    }
}

The Number class is abstract and cannot be used the same was as the classes that extend it. Number类是抽象的,不能与扩展它的类一样使用。

For instance: 例如:

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

You need to compare some value of these Number objects. 您需要比较这些Number对象的一些值。

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

So to fix your code your statement should read if(o.data.doubleValue() > this.data.longValue()) 因此,要修复您的代码,您的语句应读取if(o.data.doubleValue() > this.data.longValue())

So I'm guessing that Java does not auto unbox Number 所以我猜Java不会自动取消装箱

That is correct. 那是对的。 Unboxing is only possible when the static type of the expression you are attempting to use is one of the primitive wrapper types; 仅当您尝试使用的静态表达式类型是原始包装类型之一时,才可以进行拆箱。 eg 例如

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

but is there a way to do it? 但是有办法吗?

If you know that the Number is a specific type, then you can cast it as above. 如果您知道Number是特定类型,则可以按上述方法进行转换。 Otherwise, you can call the respective ...Value() method explicitly. 否则,您可以显式调用各自的...Value()方法。

    int j = 1 + n.intValue();

However, there is no way that you can make the operators ('+', '>' etcetera) behave as if they were overloaded for the Number class. 但是,您无法使运算符(“ +”,“>”等)表现得好像对Number类而言已重载。

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

You can use the intValue() method on node Type which will return you integer value. 您可以在节点Type上使用intValue()方法,该方法将返回整数值。 Similar methods are also available for other data types (doubleValue(), longValue() etc.). 其他数据类型(doubleValue(),longValue()等)也可以使用类似的方法。

Following will be the code snippet look like: 以下是代码片段的样子:

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;
    }
}

You should use the intValue() or method (or any of its other versions, if you want eg long or double . Also works if it isn't an Integer . 如果要使用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;
    }
} 

Although what you ask is not possible in general, in this case you can use the fact that all subclasses of Number also implement Comparable. 虽然通常您无法提出要求,但是在这种情况下,您可以使用Number的所有子类也实现Comparable的事实。 If you add & Comparable to the definition of T you can simply compare the values. 如果在T的定义中添加&可比,则可以简单地比较这些值。

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

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