@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.

  • 解决方法:其实并不能说这个答案的方向错了,而是不够详细。。。但也许是有(我)人(太)解(菜)决(菜)了。这里应该点击右上角红色的部分:
    @Override注解报错,The method xxx of type xxx must override a superclass method_第1张图片
    那里可以改变具体每个项目的编译设置,点进去找到我正在写的项目,发现编译环境确实是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的版本即可:
    @Override注解报错,The method xxx of type xxx must override a superclass method_第2张图片

你可能感兴趣的:(eclipse)