在功能接口中找不到目标方法

No target method found in functional interface

我正在尝试了解有关 Java 8 FunctionalInterface 注释的更多信息。我写了下面的代码作为实验,但它没有编译:

@FunctionalInterface
public interface HasToString {

    String toString();
}

No target method found

有趣的是, 编译:

@FunctionalInterface
public interface HasToString {

    String notToString();
}

这是为什么?

这在 JLS 9.8

中有说明

A functional interface is an interface that has just one abstract method (aside from the methods of Object), and thus represents a single function contract. This "single" method may take the form of multiple abstract methods with override-equivalent signatures inherited from superinterfaces; in this case, the inherited methods logically represent a single method.

由于 toString 是“class 对象 的“public 实例方法”,您的接口不符合成为功能界面。