标签:用途 stat font 不用 存在 9.png 加载 关于 客户
//没有参数的annotation定义
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MyAnnotationNoValue { }
//有参数的annotation定义
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MyAnnotationMultiValue { String hello() default "I‘m the default value"; int number() default 0; int [] arrays() default {1,2,3}; }
//annotation测试代码
public class AnnotationTest { @MyAnnotationNoValue public void annotationNoValue(){ } @MyAnnotationSingleValue("the single value") public void annotationSingleValue(){ } @MyAnnotationMultiValue(hello="I‘m joy, hello", number = 123, arrays = {6,6,6}) public void annotationMultiValue(){ } public static void main(String args[]) throws Exception{ AnnotationTest test = new AnnotationTest(); Method method = AnnotationTest.class.getMethod("annotationNoValue", null); if(method.isAnnotationPresent(MyAnnotationNoValue.class)){ System.out.println("no value annotation" + method.getAnnotation(MyAnnotationNoValue.class)); } method = AnnotationTest.class.getMethod("annotationSingleValue", null); if(method.isAnnotationPresent(MyAnnotationSingleValue.class)){ System.out.println("one value annotation" + method.getAnnotation(MyAnnotationSingleValue.class).value()); } method = AnnotationTest.class.getMethod("annotationMultiValue", null); if(method.isAnnotationPresent(MyAnnotationMultiValue.class)){ System.out.println("one value annotation" + method.getAnnotation(MyAnnotationMultiValue.class).hello()); System.out.println("one value annotation" + method.getAnnotation(MyAnnotationMultiValue.class).number()); System.out.println("one value annotation" + method.getAnnotation(MyAnnotationMultiValue.class).arrays().toString()); } } }
标签:用途 stat font 不用 存在 9.png 加载 关于 客户
原文地址:https://www.cnblogs.com/ximixuexi/p/10396669.html