如注释中所标记的,注释密钥owners
的默认值""
将创建一个数组,其元素为空字符串。你也可以把一个字符串"default"
在那里,以及这将导致只是为了如果需要为空数组创建仅值“默认”的数组
OR
{}
。
为了测试这一点,你可以标记与运行时保留策略注解为:
@Unfinished("Just articleware")
@Retention(RetentionPolicy.RUNTIME) // mark with Runtime retention policy
public @interface Unfinished {
enum Priority {LOW, MEDIUM, HIGH}
String value();
String[] owners() default "";
Priority priority() default Priority.MEDIUM;
}
注释本
@Unfinished(value = "")
public class AnnotatedClass {
public static void main(String[] args) {
System.out.println("");
}
}
,然后使用反射来获取的值的类Unfinished
的密钥为:
public static void main(String[] args) throws Exception {
System.out.println("Testing...");
Class<AnnotatedClass> obj = AnnotatedClass.class;
if (obj.isAnnotationPresent(Unfinished.class)) {
Annotation annotation = obj.getAnnotation(Unfinished.class);
Unfinished unfinished = (Unfinished) annotation;
System.out.println(Arrays.toString(unfinished.owners()));
}
}
两个版本都可以接受。 'default“”'就像'default {“”}'所以默认值是* 1 *元素的数组:空字符串。如果你设置了'default {}',那么默认值是* empty * array。 – Pshemo
带有空字符串的数组。你也可以在那里放一个单一的字符串“default”,或者简单地为一个空数组。 – nullpointer