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

在Java中,是否可以将数字取消装箱到适当的类型?,java,Java,我写了上面的代码,当我试图编译它时,我得到了 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

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

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.node弄糊涂了,类节点嵌入在类队列中

所以我猜Java不会自动取消对数字的装箱,但有没有办法呢

谢谢

您可以使用doubleValue:

您可以使用doubleValue:

Number类是抽象类,不能与扩展它的类相同使用

例如:

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;
    }
}
您需要比较这些数字对象的一些值

// this doesnt work
Number a = 22;
Number b = 33;
Number c = a - b; // compile error
if(a > b) // compile error
因此,要修复代码,您的语句应该读取ifo.data.doubleValue>this.data.longValue

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

例如:

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;
    }
}
您需要比较这些数字对象的一些值

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

因此,要修复代码,您的语句应该读取ifo.data.doubleValue>this.data.longValue

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

下面是代码段的外观:

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

可以对节点类型使用intValue方法,该方法将返回整数值。类似的方法也可用于其他数据类型doubleValue、longValue等

下面是代码段的外观:

// this works
Number a = 22;
Number b = 33;
Number c = a.doubleValue() - b.doubleValue();
if(a.longValue() > b.longValue())
如果需要,例如long或double,则应使用或方法或其任何其他版本。如果它不是整数,也可以使用

如果需要,例如long或double,则应使用或方法或其任何其他版本。如果它不是整数,也可以使用

所以我猜Java不会自动取消编号

这是正确的。只有当试图使用的表达式的静态类型是原始包装类型之一时,才能取消装箱;e、 g

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;
    }
} 
但有办法做到这一点吗

如果您知道数字是一种特定类型,则可以如上所述对其进行强制转换。否则,可以显式调用相应的…Value方法

    Number n = ...
    int i = 1 + n;               // Compilation error
    int j = 1 + ((Integer) n);   // OK ... provided the cast is going to work.
但是,您无法使运算符“+”、“>”等表现得好像它们是为Number类重载的

所以我猜Java不会自动取消编号

这是正确的。只有当试图使用的表达式的静态类型是原始包装类型之一时,才能取消装箱;e、 g

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;
    }
} 
但有办法做到这一点吗

如果您知道数字是一种特定类型,则可以如上所述对其进行强制转换。否则,可以显式调用相应的…Value方法

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

但是,您无法使运算符“+”、“>”等表现为它们对于Number类重载。

不要麻烦将t视为数字:使t扩展为可比较的,并将其实现为返回此.data.compareToo.data。

不要麻烦将t视为数字:使t扩展为可比较的,并将其实现为返回this.data.compareToo.data。

尽管您通常不可能这样做,但在这种情况下,您可以使用以下事实:Number的所有子类也实现Comparable。
如果您将&compariable添加到T的定义中,您可以简单地比较这些值。

尽管您的要求通常是不可能的,但在这种情况下,您可以使用以下事实:所有Number子类也实现compariable。
如果将&compariable添加到T的定义中,您可以简单地比较值。

我确信有更好的方法,但我会使用反射检测T的类型,然后为每种类型编写一个case语句,并使用intValue、longValue等方法。我确信有更好的方法,但我会使用反射检测T的类型,然后为每种类型编写一个case语句,并使用intValue、longValue等方法。