@interface
定义注解public @interface MyAnnotation{
}
@interface MyAnnotation1 {
// 参数命名 参数类型 + 参数名称 ()
// name字段 String类型 默认值为 wwbao 可以自定义
String name() default "wwbao";
// age字段 int类型 默认值 0 可以自定义
int age() default 0;
// scores字段 String 数组类型 没有默认值
String[] scores();
}
package com.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@MyAnnotation1(name = "yyll",age = 20,scores = {"语文90","100"})
public class Demo03 {
}
@Target({ElementType.TYPE, ElementType.METHOD}) // 该注解只能在类或者接口 或者方法上使用
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation1 {
// 参数命名 参数类型 + 参数名称 ()
// name字段 String类型 默认值为 wwbao 可以自定义
String name() default "wwbao";
// age字段 int类型 默认值 0 可以自定义
int age() default 0;
// scores字段 String 数组类型 没有默认值
String[] scores();
}
@interface
来定义注解value
@Target
来指定Annotation
可以应用的范围;@Retention(RetentionPolicy.RUNTIME)
便于运行期读取该Annotation
@Retention
不存在,则该Annotation
默认为CLASS
原文:https://www.cnblogs.com/juanbao/p/15032405.html