public class CommonEaoImpl extends BasicEao implements ICommonEao { @Override public void save(Object entity) { em.persist(entity); } @Override public <T> void saveList(List<T> entitys) { for (Object entity : entitys) { em.persist(entity); } } }
从代码来看没有错误,但是出现错误,如下:
The method save(Object) of type CommonEaoImpl must override a superclass method代码不再多做解释,错误简单翻译一下:父类中必须要有save方法,子类才能重写。现有情况是虽然BasicEao.java中没有save方法,但是在ICommonEao中确有save的声明:
void save(Object entity);单纯解决这个问题很简单,去掉子类中@Override,代码即可正确编译。但是我觉得我们还可以再多想一点:为什么删了就可以?
经查阅发现,出错的地方不在代码,而在jdk,这些代码在jdk1.6中可以正确编译,但是在jdk1.5中就会报出@Override错误,原因在于:jdk1.5中@Override只适用于类的继承,而不适用于接口的实现;而jdk1.6中的@Override既支持实现又支持继承。
所以现在解决方法就可以有两种:
Indicates that a method declaration is intended to override a method declaration in a superclass. If a method is annotated with this annotation type but does not override a superclass method, compilers
are required to generate an error message.
通过这段时间的英语学习,我觉得你可以尝试翻译一下。
Java中@Override问题及解决,布布扣,bubuko.com
原文:http://blog.csdn.net/lidatgb/article/details/21184157