import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation { String value(); } @MyAnnotation("This is a test") public class MyClass { // Class implementation } import java.lang.annotation.Annotation; import java.lang.reflect.Field; public class AnnotationExample { public static void main(String[] args) { Class<MyClass> clazz = MyClass.class; Annotation[] annotations = clazz.getAnnotations(); for (Annotation annotation : annotations) { if (annotation instanceof MyAnnotation) { MyAnnotation myAnnotation = (MyAnnotation) annotation; System.out.println("Value: " + myAnnotation.value()); } } } }


上一篇:
下一篇:
切换中文