@FunctionalInterface
用途:用户告知编译器,检查这个接口,保证该接口是函数式接口,即只能包含一个抽象方法,否则就会编译出错。
@FunctionalInterface
public interface fiDemo {
String demo();
}
//错误方式
@FunctionalInterface
public interface fiDemo {
String demo();
String demo1();
}
@Documented
@Documented 用户指定被该元Annotation修饰的Annotation类将会被javadoc工具
提取成文档,如果定义Annotation类时使用了@Documented 修饰,则所有使用
该Annotation修饰的程序元素的程序元素的API文档将会包含该Annotation说明
@遺伝性の
@Inherited 指定被它修饰的Annotation将具有继承性
@保持
@Retention 表示该注解类型的注解保留的时长。
1.SOURCE:仅存在java源文件,经过编译器后便丢弃相应的注解
2.CLASS:存在Java源文件,以及经编译器后生成Class字节码文件,但是运行时VM不再
保留注解。
3.RUNTIME:存在源文件,编译生成的Class文件以及保留在运行时VM中,可通过反射性
地读取注解。
@目標
@Target:表示该注解类型的所适用的程序元素类型。
1.ANNOTATION_TYPE:注解类型声明
2.CONSTRUCTOR:构造方法
3.FIELD:字段声明
4.LOCAL_VARIABLE:局部变量声明
5.METHOD:方法声明
6.PACKAGE:包声明
7.PARAMETER:参数声明
8.TYPE:类、接口或枚举声明
例
@Documented
@Target(ElementType.METHOD)
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodAnnotation {
String value() default "";
int number() default 1;
}
@MethodAnnotation(value = "我传了一个值" ,number = 111)
private static void methodAnnotation() {
}
private static void testMain() {
try {
Class<Main> mainClass = Main.class;
Method method = mainClass.getDeclaredMethod("methodAnnotation");
MethodAnnotation methodAnnotation = method.getAnnotation(MethodAnnotation.class);
System.out.println(methodAnnotation.number()+","+methodAnnotation.value());
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
結果の印刷:
111,我传了一个值