繁体   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