import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method; @Retention(RetentionPolicy.RUNTIME) @interface MyAnnotation { String value(); } class MyClass { @MyAnnotation("Hello World") public void myMethod() { // do something } } public class Main { public static void main(String[] args) { MyClass obj = new MyClass(); Class<?> cls = obj.getClass(); Method[] methods = cls.getMethods(); for (Method method : methods) { if (method.isAnnotationPresent(MyAnnotation.class)) { MyAnnotation annotation = method.getAnnotation(MyAnnotation.class); System.out.println("Annotation value: " + annotation.value()); } } } }


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