Annotation(注解@)
JDK中内置注解:
@Override //重定向X个方法
@Deprecated //过时
@SuppressWarnings(value={"unchecked"}) //压制警告
作用:
通知编绎器检测,而不是通知程序员
与注释不同,注释是通知程序员
自定义注解:
public @interface MyAnnotation {
//只能定义属性,不能定义方法
String username();
int age();
}
注解的生命周期
@Retention(RetentionPolicy.RUNTIME)//元注解,JVM运行时可见,常用
@Retention(RetentionPolicy.CLASS)//元注解,class和source中可见
@Retention(RetentionPolicy.SOURCE)//元注解,source可见
注解的范围:
@Target(value={FIELD,TYPE,METHOD,CONSTRUCTOR,PARAMETER,ANNOTATION_TYPE})//注解的范围
导入的包分别为:
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;//默认值
//创建注解,声明默认属性值
public @interface YouAnnotation {
String username() default "mary";
int age() default 12 ;
String[] value() default {"1","2","3"} ;
}
//创建注解
public @interface MyAnnotation {
//只能定义属性,不能定义方法
String username();
int age();
}
Type 是 Java 编程语言中所有类型的公共高级接口。
它们包括原始类型、参数化类型、数组类型、类型变量和基本类型。