# Annotations
Java 8中的注解是可重復的。讓我們直接深入看看例子,弄明白它是什么意思。
首先,我們定義一個包裝注解,它包括了一個實際注解的數組
```
@interface Hints {
Hint[] value();
}
@Repeatable(Hints.class)
@interface Hint {
String value();
}
```
只要在前面加上注解名:@Repeatable,Java 8 允許我們對同一類型使用多重注解,
變體1:使用注解容器(老方法)
```
@Hints({@Hint("hint1"), @Hint("hint2")})
class Person {}
```
變體2:使用可重復注解(新方法)
```
@Hint("hint1")
@Hint("hint2")
class Person {}
```
使用變體2,Java編譯器能夠在內部自動對@Hint進行設置。這對于通過反射來讀取注解信息來說,是非常重要的。
```
Hint hint = Person.class.getAnnotation(Hint.class);
System.out.println(hint); // null
Hints hints1 = Person.class.getAnnotation(Hints.class);
System.out.println(hints1.value().length); // 2
Hint[] hints2 = Person.class.getAnnotationsByType(Hint.class);
System.out.println(hints2.length); // 2
```
盡管我們絕對不會在Person類上聲明@Hints注解,但是它的信息仍然可以通過getAnnotation(Hints.class)來讀取。并且,getAnnotationsByType方法會更方便,因為它賦予了所有@Hints注解標注的方法直接的訪問權限。
```
@Target({ElementType.TYPE_PARAMETER, ElementType.TYPE_USE})
@interface MyAnnotation {}
```