如何在Java Annotation中设置String Array

Jak*_*ter 66 java annotations

我已经声明了这样的注释:

public @interface CustomAnnot 
{
    String[] author() default "me";
    String description() default "";
}
Run Code Online (Sandbox Code Playgroud)

因此有效的注释将是

@CustomAnnot(author="author1", description="test")
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚的是,如何设置多个作者,因为author()返回String []这应该是可能的.

@CustomAnnot(author="author1","autor2", description="test")
Run Code Online (Sandbox Code Playgroud)

不起作用!

Kai*_*nad 117

像这样做:

public @interface CustomAnnot {

    String[] author() default "me";
    String description() default "";

}
Run Code Online (Sandbox Code Playgroud)

和你的注释:

    @CustomAnnot(author={"author1","author2"}, description="test")
Run Code Online (Sandbox Code Playgroud)