3

我如何才能访问类中的main真假? 我应该在主课上写什么? checkSample

    package annotation;

    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;

    @Retention(RetentionPolicy.RUNTIME)

    public @interface annotation {
        public String name() default "Jimmy";
        public boolean check() default false;
    }

    package annotation;

    @annotation(name = "Jack", check = false)

    public class Sample {

        public String str = "Hi";

        public void printHi(String str) {
            System.out.println(str);
        }
    }

    package annotation;

    public class Main {
        public static void main(String[] args) {

        }
    }
4

1 回答 1

7

用于Sample.class.getAnnotation(annotation.class)获取您的注释实例,并调用check()以获取检查值:

System.out.println(Sample.class.getAnnotation(annotation.class).check());

请注意,类应该以大写字母开头,并且将注释命名为“annotation”非常令人困惑。

于 2012-07-26T15:02:39.640 回答