[英]creating custom annotation and using it in java?
我已經在“自定義注釋”下面寫了。
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value();
}
並使用以下注釋。
@MyAnnotation("someValue")
public void someMethod(){
}
上面的代碼工作正常,沒有任何問題。 但是在注釋類中,我必須重命名value()方法名稱。 我可以做以下事情嗎?
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String name();
}
我試着做,但日食給編譯錯誤。
- The attribute value is undefined for the annotation type
MyAnnotation
- The annotation @MyAnnotation must define the attribute
name
任何原因?
像這樣使用它:
@MyAnnotation(name="someValue")
public void someMethod(){
}
因為默認情況下注解具有值方法,所以如果您這樣指定
@MyAnnotation("someValue")
public void someMethod(){
}
默認情況下,它將value="someValue"
為value="someValue"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.