你可以将 MyAnnotation 注解的 value 属性修改为 "111",如下所示:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface MyAnnotation {
String value() default "";
}
public class Test {
@MyAnnotation(value = "111")
private String myField;
public static void main(String[] args) throws NoSuchFieldException {
Class<?> cls = Test.class;
MyAnnotation annotation = cls.getDeclaredField("myField").getAnnotation(MyAnnotation.class);
System.out.println(annotation.value()); // 输出:111
}
}
以上代码中,将 @MyAnnotation(value = "example") 修改为 @MyAnnotation(value = "111")。运行时打印结果将是 "111"。
内容由零声教学AI助手提供,问题来源于学员提问




