JAVA注解

来源:互联网 发布:字中找字软件下载 编辑:程序博客网 时间:2024/06/06 04:31

Java的预定义注解:

@override 覆盖父类方法

@Deprecated  不推荐使用的

@SuppressWarnings 抑制编译器产生警告信息的

 

Java4个元注解:

TargetRetentionDocumentedInherited

元注解可以注解包括:类,接口,枚举类型,注解

 

Java的自定义注解:

下面这个例子包括了如何使用自定义注解和四个元注解

public class Test{ @myAnnotation("this is an annotation")public void t(){}}@Target({ElementType.METHOD})//只运行注解方法@interface myAnnotation1{String value();}@Retention(RetentionPolicy.RUNTIME)//可以通过反射得到注解:Annotation annotation = TestAnnotation.class.getAnnotation(MyAnnotation.class);@interface myAnnotation21{}@Retention(RetentionPolicy.SOURCE)//不将注解保存在class文件中@interface myAnnotation22{}@Retention(RetentionPolicy.CLASS)//只将注解保存在class文件中,而使用反射读取注解时忽略这些注解@interface myAnnotation23{}@Documented//在生成java doc文档的时候,会显示注解@interface myAnnotation3{}@Inherited//可以被子类继承@interface myAnnotation4{}


0 0
原创粉丝点击