なぜ二つの方法で自分の@FunctionalInterfaceには、コンパイルエラーはありませんか?

ニティンT:

Javaの8で有効な機能インターフェースインターフェース以下ですか?

@FunctionalInterface
interface Normal{
    public abstract String move();
    public abstract String toString() ;
}

なぜそれが私のコンパイル時のエラーを与えるものではありませんか?

kriegaex:

Alokが引用されたことは事実であるが、彼は間違った(コードが無効であることを)彼の最終的な答えを作る何かを、見落とし:

インターフェースは、一つの方法があるString toString()からそれを継承するすべてのクラスがすでに実装し、Objectすなわち宣言インターフェイスメソッドは、すでにデフォルトの方法と同様の実装を持っています。したがって、そこにはコンパイルエラーがなく、Normal私に示すように、機能インタフェースとして使用することができるMCVE

package de.scrum_master.stackoverflow;

@FunctionalInterface
interface Normal {
  String move();
  String toString();
}

ところで、とインタフェースメソッドを宣言する必要はありませんpublic、彼らは常にあるので。同じことがのために行きますabstract

package de.scrum_master.stackoverflow;

public class NormalApp {
  static void doSomething(Normal normal) {
    System.out.println(normal.move());
    System.out.println(normal.toString());
  }

  public static void main(String[] args) {
    doSomething(() -> "xxx");
  }
}

あなたは、ドライバのアプリケーションを実行する場合は、このコンソールログが表示されます。

xxx
de.scrum_master.stackoverflow.NormalApp$$Lambda$1/1530388690@28c97a5

あなたはメソッド名を変更した場合さてtoString、何か他に例をtoStringX、あなたが起因していることがわかります@FunctionalInterface:クラスをコンパイルするときに予想されるエラーメッセージがあります

Unexpected @FunctionalInterface annotation
  de.scrum_master.stackoverflow.Normal is not a functional interface
    multiple non-overriding abstract methods found in interface de.scrum_master.stackoverflow.Normal

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=3723&siteId=1