不实现接口的所有方法 [英] Not implementing all the methods of an interface

查看:650
本文介绍了不实现接口的所有方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试重现代码以下。我得到一个错误告诉我,我必须实现所有继承的方法(因为比较器是一个接口)。


类型 new Comparator(){} 必须实现继承的抽象方法 Comparator.reversed()


有很多这些方法,我唯一要覆盖的是比较。我必须实现所有其他方法,还是有一种方式来指定我不需要实现它们?
了解因为接口的合同性质,我必须这样做,但是如果我只需要改变一种方法呢?

  static Map sortByValue(Map map){
列表列表= new LinkedList(map.entrySet());
Collections.sort(list,new Comparator(){
public int compare(Object o1,Object o2){
return((Comparable)((Map.Entry)(o1))) getValue())
.compareTo(((Map.Entry)(o2))。getValue());
}
});

映射结果= new LinkedHashMap();
for(Iterator it = list.iterator(); it.hasNext();){
Map.Entry entry =(Map.Entry)it.next();
result.put(entry.getKey(),entry.getValue());
}
返回结果;
}

编辑
通过更改eclipse中的java8符合级别。谢谢!

解决方案


类型 new Comparator(){} code>必须实现继承的抽象方法 Comparator.reversed()然后如果我应用修复,我添加了很多功能


Comparator.reversed 在Java 1.8中引入,它是一个默认方法,即您不要的方法必须覆盖。 >

似乎您的合规级别设置为Java 1.8之前(因为Eclipse要求您覆盖反向),而使用Java 1.8 API(因为 Comparator 有一个反转方法)。



确保您将API更改为1.7 或将合规级别更改为1.8 。 (后一个选项需要Eclipse Luna或更好)。



更多关于Eclipse合规性级别:
什么是编译器合规性级别在Eclipse中?


I tried reproducing the code below on eclipse. I get an error telling me that I have to implement all the inherited methods (because Comparator is an interface).

The type new Comparator(){} must implement the inherited abstract method Comparator.reversed().

There are many of these methods and the only one I want to overwrite is compare. Do I have to implement all the other methods or is there a way to specify I don't need to implement them? I understand that I will have to do it because of the contractual nature of an interface, but what if I just need to change one method?

static Map sortByValue(Map map) {
     List list = new LinkedList(map.entrySet());
     Collections.sort(list, new Comparator() {
          public int compare(Object o1, Object o2) {
               return ((Comparable) ((Map.Entry) (o1)).getValue())
              .compareTo(((Map.Entry) (o2)).getValue());
          }
     });

    Map result = new LinkedHashMap();
    for (Iterator it = list.iterator(); it.hasNext();) {
        Map.Entry entry = (Map.Entry)it.next();
        result.put(entry.getKey(), entry.getValue());
    }
    return result;
} 

EDIT Solved by changing the compliance level to java8 in eclipse luna. Thanks!

解决方案

The type new Comparator(){} must implement the inherited abstract method Comparator.reversed() then if I apply the fix, I have many functions added

Comparator.reversed was introduced in Java 1.8 and it's a default method i.e. a method that you don't have to override.

It seems like you have your compliance level set to pre Java 1.8 (since Eclipse asks you to override reversed), while using Java 1.8 API (since Comparator has a reversed method).

Make sure you either change your API to 1.7 or change your compliance level to 1.8. (The latter option requires Eclipse Luna or better.)

More on Eclipse compliance level: What is "compiler compliance level" in Eclipse?

这篇关于不实现接口的所有方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆