简体   繁体   English

将接口类型与compareTo()一起使用-错误(java)

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

Here is my Item class, which implements Thing (my interface) 这是我的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.
}

Here is the interface 这是界面

package moving.domain;

public interface Thing {

    int getVolume();               

}    

and here is the main method 这是主要方法

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

    }
}

When I try to sort the collection I get the following error: 当我尝试对集合进行排序时,出现以下错误:

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>)

I got the same error when I tried to create the same program as shown in section 45 of the following: https://materiaalit.github.io/2013-oo-programming/part2/week-9/ 尝试创建以下第45节中所示的相同程序时,遇到了相同的错误: https : //materiaalit.github.io/2013-oo-programming/part2/week-9/

I then recreated the same situation in the above code to see if it happened again and it did. 然后,我在上面的代码中重新创建了相同的情况,以查看是否再次发生并且确实如此。 It's really bugging me and I can't seem to understand what the problem is. 这真的使我烦恼,我似乎无法理解问题所在。 In the end I even copied and pasted all of the code from section 45 and it still gave the same error, and it gives it here. 最后,我什至复制并粘贴了第45节中的所有代码,仍然给出了相同的错误,并在此处给出。 Can interface types be sorted using an interface reference? 可以使用接口引用对接口类型进行排序吗? Am I missing something obvious? 我是否缺少明显的东西? If I change all the type declarations to Item it works. 如果我将所有类型声明都更改为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);

}

I get the same error when I create an ArrayList of things, as above. 如上所述,当我创建事物的ArrayList时遇到相同的错误。

Your error occurs because Thing s are not necessarily Comparable . 发生错误是因为Thing不一定是Comparable The only class you've made implements both Thing and Comparable<Thing> , but you could easily create: 您制作的唯一类同时实现ThingComparable<Thing> ,但是您可以轻松创建:

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

It's not Comparable . 这并不Comparable It illustrates the possibility that the compiler sees when you attempt to sort a List<Thing> . 它说明了当您尝试对List<Thing>进行排序时,编译器会看到的可能性。 Thing s aren't necessarily Comparable . Thing s为不一定Comparable

You could change the list to a List<Item> if you want, because you've defined Item s to be Comparable . 如果需要,可以将列表更改为List<Item> ,因为已将Item定义为Comparable Alternatively, you could have the Thing interface extend Comparable<Thing> . 或者,您可以让Thing接口扩展Comparable<Thing> Another option is to pass a Comparator<Thing> as a second argument to Collections.sort , as a way to compare objects that aren't Comparable . 另一个选择是将Comparator<Thing>作为第二个参数传递给Collections.sort ,作为比较不Comparable对象的方法。

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

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