簡體   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