原文链接:http://blog.sina.com.cn/s/blog_4b6d137b0100mu0m.html
最近我的Eclipse在关于Annotation的问题上老是莫名其妙的出些问题,最常见的就是关于Annotation中的@Override这个标记了。
按照定义@Override在java中表示覆写一个基类中的方法,其在源码中的定义如下
/**
* 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.
*
也就是说在基类中声明的method在继承类中实现的话久可以用@Override进行标注。但是问题时如果我的method时定义在一个interface中间而不是abstract class时这个标注会不会报错了?
我开始在机器中装的是jdk1.6,进行开发的时候定义接口如下:
public interface AttachmentManager {
public void saveAttachment(Attachment attachment);
public void saveAttachment(AttachmentVO attachmentVO, String collegeId);
public Attachment getAttachment(String id);
public void deleteAttachment(String id);
}
然后实现这个接口:
public class AttachmentManagerImpl implements AttachmentManager {
private AttachmentDao attachmentDao;
public void saveAttachment(Attachment attachment) {
attachmentDao.saveAttachment(attachment);
}
@Override
public void setAttachmentDao(AttachmentDao attachmentDao) {
this.attachmentDao = attachmentDao;
}
@Override
public void saveAttachment(AttachmentVO attachmentVO, String collegeId) {
MultipartFile data = attachmentVO.getData();
Attachment attachment = new Attachment();
try {
attachment = AttachmentUtil.createAttachment(data,collegeId);
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
attachmentDao.saveAttachment(attachment);
}
@Override
public Attachment getAttachment(String id) {
Attachment attachment = attachmentDao.getAttachment(id);
return attachment;
}
@Override
public void deleteAttachment(String id) {
Attachment attachment = getAttachment(id);
AttachmentUtil.deleteAttachment(attachment);
attachmentDao.deleteAttachment(attachment);
}
}
可以正常编译和运行,但是前几天Eclipse出了点小问题,然后就无法运行,提示的是无法找到method,当我把@Override删除以后一切正常,加上后又是一样。然后我把jdk卸载装了1.5,问题依旧,我又在命令行下进行编译还是一样,于是我将1.6重新装回,将Eclipse重新安装,运行成功,不再报错。
我上网查了查这个问题,大致的原因和我所碰到的情况一致,@override注释在jdk1.5环境下只能用于对继承的类的方法的重写,而不能用于对实现的接口中的方法的实现。
但是我之前装的1.6版本为什么会出现这个问题我只能认为是我的eclipse的原因了,只有天知道。
特别说明:本人转载文章纯为技术学习,总结经验,并无其他目的,若有他人继续转载,请链接原作者的地址,而不是本文的地址,以示对作者的尊重。最后对原作者的辛勤劳动表示感谢!
分享到:
相关推荐
NULL 博文链接:https://justshare.iteye.com/blog/431424
Annotation 注解 新特性 jdk1.6 jdk1.5 很详细的学习资料
Java学习笔记 台湾 林信良 什么是Java 从autoboxing、unboxing认识对象 语法入门 Annotation 文本编辑器 Java SE6新功能简介 继承、多型
NULL 博文链接:https://stillcoolme.iteye.com/blog/2314109
java.awt.dnd Drag 和 Drop 是一种直接操作动作,在许多图形用户界面系统中都会遇到它,它提供了一种机制,能够在两个与 GUI 中显示元素逻辑相关的实体之间传输信息。 java.awt.event 提供处理由 AWT 组件所激发的...
内含jdk1.6z中文版API+JavaEEAPI+JavaScriptAPI+servletAPI帮助文档,MySQL帮助文档,w3school帮助文档,里面涵盖了Java学习当中用到的帮助文档
解决“annotation-config are only available on JDK 1.5 and higher”的问题!
JDK5.0 - Java Annotation 介绍 (ppt格式)
JDK1.6的九大新特性 九:Common Annotations 八:对脚本语言的支持 七:用Console开发控制台程序 六:插入式注解处理API(Pluggable Annotation Processing API) 五:轻量级Http Server API 四:使用Compiler API 三...
JDK9及以上版本没有javax.annotation-api-***.jar包 ,无法使用注解:@Resource JDK新特性,高版本JDK没有自带的javax(java扩展包)了。或者是使用的JDK不完整。 下载javax.annotation.jar包,导入到lib文件夹下,...
java1.5 annotation注释源代码,
JDK5.0-Java Annotation 介绍
Annotation java1.5 jdk.15 简单介绍
java.awt.dnd Drag 和 Drop 是一种直接操作动作,在许多图形用户界面系统中都会遇到它,它提供了一种机制,能够在两个与 GUI 中显示元素逻辑相关的实体之间传输信息。 java.awt.event 提供处理由 AWT 组件所激发的...
The Java Development Kit (JDK) APIs are specific to the JDK and will not necessarily be available in all implementations of the Java SE Platform. These APIs are in modules whose names start with jdk....
NULL 博文链接:https://lansky07.iteye.com/blog/289007
jdk1.6新特性 1.Desktop类和SystemTray类 2.使用JAXB2来实现对象与XML之间的映射 3.StAX 4.使用Compiler API 5.轻量级Http Server API 6.插入式注解处理API(Pluggable Annotation Processing API) 7.用Console开发...