Java 如何为自己的注释创建可选参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3520375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create optional parameters for own annotations?
提问by Biju CD
Following is the annotation code
以下是注释代码
public @interface ColumnName {
String value();
String datatype();
}
I would like to make datatype
an optional parameter, for example
我想做datatype
一个可选参数,例如
@ColumnName(value="password")
should be a valid code.
应该是有效的代码。
采纳答案by Riduidel
Seems like the first example in the official documentationsays it all ...
/**
* Describes the Request-For-Enhancement(RFE) that led
* to the presence of the annotated API element.
*/
public @interface RequestForEnhancement {
int id();
String synopsis();
String engineer() default "[unassigned]";
String date() default "[unimplemented]";
}
回答by Johannes Wachter
To make it optional you can assign it a default value like that:
要使其可选,您可以为它分配一个默认值,如下所示:
public @interface ColumnName {
String value();
String datatype() default "String";
}
Then it doesn't need to be specified when using the Annotation.
那么在使用 Annotation 时就不需要指定了。