简体   繁体   English

没有实现接口的所有方法

[英]Not implementing all the methods of an interface

I tried reproducing the code below on eclipse. 我尝试在eclipse上复制下面的代码。 I get an error telling me that I have to implement all the inherited methods (because Comparator is an interface). 我收到一个错误,告诉我必须实现所有继承的方法(因为Comparator是一个接口)。

The type new Comparator(){} must implement the inherited abstract method Comparator.reversed() . new Comparator(){}必须实现继承的抽象方法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. 编辑通过在eclipse luna中将合规性级别更改为java8来解决。 Thanks! 谢谢!

The type new Comparator(){} must implement the inherited abstract method Comparator.reversed() then if I apply the fix, I have many functions added 类型new Comparator(){}必须实现继承的抽象方法Comparator.reversed()然后如果我应用修复,我添加了很多函数

Comparator.reversed was introduced in Java 1.8 and it's a default method ie a method that you don't have to override. Comparator.reversed是在Java 1.8中引入的,它是一个默认方法,即您不必覆盖的方法。

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). 看起来您已将您的合规性级别设置为Java 1.8之前(因为Eclipse要求您覆盖reversed ),同时使用Java 1.8 API(因为Comparator具有reversed方法)。

Make sure you either change your API to 1.7 or change your compliance level to 1.8 . 确保将API更改为1.7将合规性级别更改为1.8 (The latter option requires Eclipse Luna or better.) (后一种选择需要Eclipse Luna或更好。)

More on Eclipse compliance level: What is "compiler compliance level" in Eclipse? 有关Eclipse合规性级别的更多信息:Eclipse 中的“编译器合规级别”是什么?

This is sort of a wild guess: I think you are using Java 8 in a pre-Java 8-Eclipse (ie pre-Luna). 这是一个疯狂的猜测:我认为你在Java 8-Eclipse之前使用Java 8(即pre-Luna)。 This way, Eclipse does not know that all those new Comparator methods, like thenComparing , have a default implementation. 这样,Eclipse就不知道所有那些新的Comparator方法(如thenComparing )都有默认实现。

In Java 7 , Comparator has just the method compare , while in Java 8 in has a whole lot more methods, all of which have a default implementation directly in the interface and need not to be implemented in a subclass. 在Java 7中 ,Comparator只有方法compare ,而在Java 8中有更多的方法,所有这些方法都直接在接口中有一个默认实现,不需要在子类中实现。

I suggest you switch to the newest version of Eclipse, which should be Luna. 我建议你切换到Eclipse的最新版本,它应该是Luna。 Alternatively, you can also install a patch for Eclipse Kepler, but switching to Luna is certainly better. 或者,您也可以为Eclipse Kepler安装补丁,但切换到Luna肯定更好。

The purpose of an interface is to obligate the implementer of said interface to have all of the methods listed there. 接口的目的是强制所述接口的实现者拥有其中列出的所有方法。 Now there are a few ways to got about not implementing all of the methods. 现在有几种方法可以实现不实现所有方法。

  1. For every method you don't wish to implement (support) just throw UnsupportedOperationException . 对于您不希望实现的每个方法(支持),只需抛出UnsupportedOperationException An example of this sort of implementation would be the Collections API . 这种实现的一个例子是Collections API

To keep the number of core collection interfaces manageable, the Java platform doesn't provide separate interfaces for each variant of each collection type. 为了保持核心集合接口的数量可管理,Java平台不为每个集合类型的每个变体提供单独的接口。 (Such variants might include immutable, fixed-size, and append-only.) Instead, the modification operations in each interface are designated optional — a given implementation may elect not to support all operations. (这些变体可能包括不可变,固定大小和仅附加。)相反,每个接口中的修改操作都被指定为可选 - 给定的实现可以选择不支持所有操作。 If an unsupported operation is invoked, a collection throws an UnsupportedOperationException. 如果调用了不受支持的操作,则集合将抛出UnsupportedOperationException。 Implementations are responsible for documenting which of the optional operations they support. 实现负责记录它们支持哪些可选操作。 All of the Java platform's general-purpose implementations support all of the optional operations. 所有Java平台的通用实现都支持所有可选操作。

  1. Or you could follow what the Java developers did with some of the interfaces that declare a lot of methods eg: MouseAdapter . 或者你可以按照Java开发人员对一些声明很多方法的接口所做的事情,例如: MouseAdapter This way you'll implement every method, but they won't be doing anything useful. 这样你就可以实现每个方法,但是它们不会做任何有用的事情。

An abstract class may be better suited for what you are doing. 抽象类可能更适合您正在做的事情。 Although you could just .super() everything. 虽然你可以只是.super()一切。

You have run into an Eclipse bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=390889 你遇到了一个Eclipse bug: https//bugs.eclipse.org/bugs/show_bug.cgi?id = 390889

Basically, JAVA 1.8 introduced a brand new method Comparator.reversed(). 基本上,JAVA 1.8引入了一种全新的方法Comparator.reversed()。 And since you have a JAVA 1.7 or earlier code, JAVA 1.8 doesn't find the method and fails to compile. 由于你有一个JAVA 1.7或更早的代码,JAVA 1.8找不到该方法并且无法编译。

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

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