什么是默认“"对于注释声明中的 String[]? [英] What is default "" for String[] in annotation declaration?
本文介绍了什么是默认“"对于注释声明中的 String[]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面代码中的 default ""
部分是什么意思?
What does the default ""
part mean in the following code?
public @interface MyAnnotation {
String[] names() default "";
}
是否等于
String[] names() default new String[0];
?
推荐答案
public @interface MyAnnotation {
String[] names() default "";
}
意味着默认情况下 names
将是一个空 String
(""
) 的数组.
means that by default names
will be an array of one empty String
(""
).
这与您使用数组初始化程序的情况相同:
This is the same as if you've used the array-initializer:
public @interface MyAnnotation {
String[] names() default { "" };
}
此外,String[] names() default new String[0];
不会编译,因为注释属性的(默认)值必须是常量表达式.
Additionally, String[] names() default new String[0];
doesn't compile, because the (default) value of an annotation property must be a constant expression.
作为作业,您可以使用 MyAnnotation
注释某些内容,并使用 Reflection 探索其属性.
As a homework, you can annotate something with MyAnnotation
and explore its properties with Reflection.
这篇关于什么是默认“"对于注释声明中的 String[]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文