import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method; @Retention(RetentionPolicy.RUNTIME) @interface CustomAnnotation { String value(); } class MyClass { @CustomAnnotation("Hello") public void myMethod() { System.out.println("Executing myMethod"); } } public class Main { public static void main(String[] args) throws NoSuchMethodException { Method method = MyClass.class.getMethod("myMethod"); CustomAnnotation annotation = method.getAnnotation(CustomAnnotation.class); if (annotation != null) { System.out.println("Annotation value: " + annotation.value()); } } }


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