我定义的注解是这样的- package edu.ay.day2;
- import java.lang.annotation.*;
- @Retention(RetentionPolicy.RUNTIME)
- @Target(ElementType.METHOD)
- public @interface AnnotationDemo {
- String color() default "blue";
- String value();
- }
复制代码 下面这个程序:- package edu.ay.day2;
- import java.lang.annotation.Annotation;
- public class Annotationtest {
- /**
- * @param args
- */
- @AnnotationDemo("gjljgld")
- public static void main(String[] args)throws Exception {
- // TODO Auto-generated method stub
- System.out.println(Annotationtest.class.isAnnotationPresent(AnnotationDemo.class));//<font color="#ff0000">为什么这儿输出时false呢</font>
- if(Annotationtest.class.isAnnotationPresent(AnnotationDemo.class)){
- AnnotationDemo ann = (AnnotationDemo)Annotationtest.class.getAnnotation(AnnotationDemo.class);
- System.out.println(ann.color());
- System.out.println(ann.value());
- }
- }
- }
复制代码 |