我在自定義註釋下面寫了。創建自定義註釋並在java中使用它?
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value();
}
我正在使用下面的註釋。
@MyAnnotation("someValue")
public void someMethod(){
}
上面的代碼工作正常,沒有任何問題。 但是在註釋類中,值()方法名稱我必須重新排列。我可以做如下嗎?
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String name();
}
我試過但eclipse給出了編譯錯誤。
- The attribute value is undefined for the annotation type
MyAnnotation
- The annotation @MyAnnotation must define the attribute
name
任何原因?
由於我不是得到任何錯誤。所以請張貼您的錯誤呢? – Prateek
Prateek,我編輯了我的問題... – user755806
看看我的回答 – Prateek