1. 程式人生 > >@Override註解報錯,The method xxx of type xxx must override a superclass method

@Override註解報錯,The method xxx of type xxx must override a superclass method

  • 先交代環境:jdk1.8.0_65,eclipse版本: Oxygen Release (4.7.0)
  • 今天用到@Override註解時,發現eclipse報錯The method xxx of type xxx must override a superclass method,網上一查,基本都是同一篇答案,如下:

    java1.5中繼承介面是不需要@Override的,而在1.6中是需要新增@Override註解的,如果專案的編譯器是1.5版本的就可能報錯The method xxx of type must override a superclass method.
    解決此問題的辦法是修改java編譯器的版本,方法是:elicpse的Project選單 –> Properties –> 找到Java Compiler將Compiler Compliance level修改為1.6即可
    這裡寫圖片描述

  • 就連stackoverflow上的回答也是這個答案:

    It sounds like the JDK compliance level of your project is set to 1.5. (The version of Java installed on the machine only determines how high you can set the compliance level.) You can check this in Eclipse by opening the project properties (right click on the project name, then select “Properties”) and selecting “Java Compiler” on the left.
    In Java 1.6, the @Override annotation can (and should) be used on methods that are defined in implemented interfaces. Java 1.5 did not allow this.

  • 解決方法:其實並不能說這個答案的方向錯了,而是不夠詳細。。。但也許是有(我)人(太)解(菜)決(菜)了。這裡應該點選右上角紅色的部分:
    這裡寫圖片描述
    那裡可以改變具體每個專案的編譯設定,點進去找到我正在寫的專案,發現編譯環境確實是1.5,改成跟jdk一樣的版本1.8,Apply,KO!
  • 改完之後,@Override註解沒問題了,但是可能又出現一個錯誤,專案上出現小紅×,但是錯誤不在程式碼裡,切換到下面的Markers標籤,發現錯誤如下:
    Java compiler level does not match the version of the installed Java project facet

    這個錯誤只需開啟專案的本地資料夾,找到.settings資料夾下的org.eclipse.wst.common.project.facet.core.xml檔案,更改如下程式碼為當前使用的jdk的版本即可:
    這裡寫圖片描述