1、Annotation 注解 :给程序看的提示信息叫做注解
@Override:限定重写父类方法
@Deprecated:过时
@Suppress Warnings:抑制编译器警告
2、自定义注解:
(1)声明注解
<1>使用@interface关键字来定义注解,在这个类中可以声明注解的属性。
public @interface tran{
String name();
String name2() default "xxx";
String value();
}
注解属性的声明类似于在为接口声明一个方法,同时可以为属性设定默认值。
如果注解中只包含一个名为value的属性,则这个属性在使用时可以省略注解的名字直接写值。
<2>使用元注解对注解进行描述
@Retention:只能用于修饰一个Annotation定义,用于指定该Annotation可以保留的域。
@Target:指定被修饰的注解可以使用在什么位置,ElementType的成员变量,可以是类/方法/字段/构造方法/包声明。
@Documented:用于指定被元Annotation修饰的Annotation类将被javadoc工具提取成文档。
@Inherited:被它修饰的Annotation将具有继承性。
(2)使用注解
在@Target声明的位置上使用@Tran(属性名 = 属性值,...)。
(3) 反射注解
<T extends Annotation> T getAnnotation(Class<T> annotationClass)
如果存在该元素的指定类型的注释,则返回这些注释,否则返回null。
Annotation[] getAnnotations()
返回此元素上存在的所有注释。
Annotation[] getDeclaredAnnotations()
返回直接存在于此元素上的所有注释。
boolean is AnnotationPresent(Class <? extends Annotation> annotationClass)
如果指定类型的注释存在于此元素上,则返回true,否则返回false。
*获取到注解对象后,就像调用方法一样获取注解的属性。