- annotation 能够添加到java源代码的语法元数据类、方法、变量、参数、包都可以被注解,可用来将元数据与程序元素进行关联。
annotation分类
- 标准annotation
override,deprecated,suppressWarnings
java自带的几个annotation - 元annotation
@retention,@Target,@Inherited,@Documented
用来定义annotation的annotation
-自定义annotation
annotation自定义-调用
public class App{
@MethodInfo(
author = “”,
data = “”,
version = 1
)
public String get(){
return “”;
}
}
上面是调用自定义的annotation-MethodInfo的例子
作用:给方法添加相关信息
annotation自定义-定义
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) @Inherited
public @interface MethodInfo {
String author() default “trinea@gmail.com”;
String data();
int version() default 1;
}
- 使用@interface定义,后面时自定义注解名
元Annotation
- @Retention
– source (原码时),Class(编译时),RunTime(运行时),默认为Class - @taeget
表示该注解用来修饰哪些程序元素,如type,method,constuctor,filed,parameter等,未标注表示可修饰所有 - @inherited
表示是否可以被继承,默认为false