注解
- Annotation其实就是代码里的特殊标记,这些标记可以在编译,类加载,运行时被读取,并执行相应的处理,通过使用Annotation,程序员可以在不改变原有逻辑的情况下,在源文件中嵌入一些补充信息。
- Annotataion可以像修饰符一样被使用,可用于修饰包、类、构造器、方法、成员变量、参数,局部变量的声明,这些信息被保存在Annotation的"name=value"对中。
- 自定义注解
- 使用@interface来自定义注解
class TestB{
@TestAnnotation(id=100, desc = "姓名")
String name;
}
//@Target() -> 要注解类的什么内容, ElementType.FIELD -> 注解类的属性
//@Retention() -> 注解的运行周期
//@Documented -> 将注解放入doc文档中
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@interface TestAnnotation{
public int id() default 0;
public String desc() default "";
}