簡體   English   中英

Eclipse告訴我Long是不可比的

[英]Eclipse tells me that Long is not Comparable

我在這里處於一種奇怪的情況,即eclipse告訴我Long是“不是邊界參數<T extends Comparable<? super T>>的有效替代品”。 關於可能的原因有什么建議嗎? 我在下面粘貼相關代碼

抽象對:

public abstract class Pair<T extends Comparable<? super T>, R> implements Comparable<Pair<T, R>>{

    private T tt;
    private R rr;

    public Pair(T t, R r){
        tt = t;
        rr = r;
    }

    @Override
    public String toString(){
        return tt+ ": " +rr.toString();
    }
}



具體對:

import utilities.Pair;

public class LogBookRecord<Long, String> extends Pair<Long, String>{

    LogBookRecord(Comparable t, Object r) {
        super(t, r);
        // TODO Auto-generated constructor stub
    }
}


我嘗試將抽象類頭更改為:

public abstract class Pair<T extends Comparable<T>, R> implements Comparable<Pair<T, R>>

這沒有幫助,並且還可以:

public abstract class Pair<T, R> implements Comparable<Pair<T, R>>

但是隨后,在具體的類中,我收到一條通知,提示我應該將類型參數更改為<Comparable, Object>

public class LogBookRecord<Long, String> extends Pair<Long, String>{
                                ^                           ^
                                |                           |
      generic type variable declaration (new type names)    |
                                                  generic type arguments

該代碼等效於

public class LogBookRecord<T, R> extends Pair<T, R>{

您只需用自己的類型變量名將名稱LongString起來。

由於T沒有界限,所以它不一定是Comparable ,並且編譯器無法將其驗證為Pair類型參數。

你想要的是

public class LogBookRecord extends Pair<Long, String>{

非通用的類,它提供具體類型作為Pair超類聲明的類型參數。

Java語言規范描述了類聲明語法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM