簡體   English   中英

將接口類型與compareTo()一起使用-錯誤(java)

[英]Using an interface type with compareTo() - error (java)

這是我的Item類,它實現Thing(我的界面)

package moving.domain;

public class Item implements Thing, Comparable<Thing>{

private int volume;
private String name;

public Item (String name, int vol) {

    this.name = name;
    this.volume = vol;
}

public String getName() {

    return this.name;
}

@Override
public int getVolume() {

    return this.volume;
}

 @Override
public String toString() {

    return this.getName() + " (" + this.getVolume() + " dm^3)";
}

@Override
public int compareTo(Thing t) {
    return (int) Integer.compare(this.getVolume(),(t.getVolume())); //To change body of generated methods, choose Tools | Templates.
}

這是界面

package moving.domain;

public interface Thing {

    int getVolume();               

}    

這是主要方法

package moving;

import java.util.Collections;
import java.util.List;
import moving.domain.Item;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import moving.domain.Thing;

public class Main {

    public static void main(String[] args) {
        // test your program here
        Map<String, Thing> items2 = new HashMap<String, Thing>();
        //storing interface type Thing in hashmap as value      

        items2.put("three", new Item("passport", 2));
        items2.put("two", new Item("toothbrash", 1));
        items2.put("one", new Item("circular saw", 100));
        List<Thing> items3 = new ArrayList<Thing>(items2.values());

        Collections.sort(items3);
        //
        System.out.println(items3);

    }
}

當我嘗試對集合進行排序時,出現以下錯誤:

no suitable method found for sort(List<Thing>)

method Collections <T#1>sort<List<T#1>) is not applicable

(inferred type does not conform to upper bounds(s)

inferred: Thing

upper bounds(s) Comparable <? super Thing>)

method Collections<T#2>sort(List<T#2>,Comparator<?superT#2>)is not applicable

(cannot infer type - variables T#2

(actual and formal argument lengths differ in length)

where T#1,T#2 are type variables:

T#1 extends comparable <?super T#1> declared in method <T#1>sort(List<T#1>)

t#2 extends object declared in method <t#2>sort(List<t#2>, Comparator<?super t#2>)

嘗試創建以下第45節中所示的相同程序時,遇到了相同的錯誤: https : //materiaalit.github.io/2013-oo-programming/part2/week-9/

然后,我在上面的代碼中重新創建了相同的情況,以查看是否再次發生並且確實如此。 這真的使我煩惱,我似乎無法理解問題所在。 最后,我什至復制並粘貼了第45節中的所有代碼,仍然給出了相同的錯誤,並在此處給出。 可以使用接口引用對接口類型進行排序嗎? 我是否缺少明顯的東西? 如果我將所有類型聲明都更改為Item,那么它將起作用。

public class Main {

public static void main(String[] args) {
    // test your program here
      List<Thing> items = new ArrayList<Thing>();
items.add(new Item("passport", 2));
items.add(new Item("toothbrash", 1));
items.add(new Item("circular saw", 100));

Collections.sort(items);
System.out.println(items);

}

如上所述,當我創建事物的ArrayList時遇到相同的錯誤。

發生錯誤是因為Thing不一定是Comparable 您制作的唯一類同時實現ThingComparable<Thing> ,但是您可以輕松創建:

public class UncomparableThing implements Thing {  /* ... */ }

這並不Comparable 它說明了當您嘗試對List<Thing>進行排序時,編譯器會看到的可能性。 Thing s為不一定Comparable

如果需要,可以將列表更改為List<Item> ,因為已將Item定義為Comparable 或者,您可以讓Thing接口擴展Comparable<Thing> 另一個選擇是將Comparator<Thing>作為第二個參數傳遞給Collections.sort ,作為比較不Comparable對象的方法。

暫無
暫無

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

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