我是註解的新手。我經歷了很多教程來解釋註釋的概念。但是我找不到在一個類中定義多個註釋的信息。所以,請給我一些關於定義和訪問多個批註的見解。以下是我在一個類中定義兩個批註的代碼,而Eclipse IDE向我提供了一個錯誤「公共類型SampleAnn必須在其自己的文件中定義」。是原因對於這個錯誤,因爲Java約定「應該只有一個公共註釋每個類名稱的類名稱」?Java註釋基礎知識
@Documented
@Target(ElementType.LOCAL_VARIABLE)
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodInfo{
int number1;
}
@Documented
@Target(ElementType.LOCAL_VARIABLE)
@Retention(RetentionPolicy.RUNTIME)
public @interface SampleAnn{
int number2;
}
註解是類,你不能有每個文件多個公共頂級類在Java中。 –