作用:為數據提供描述數據
概要
系統注解:
* 內置注解
* @Deperated
* @Overrride
* @SuppressWarnings
* 元注解
* @Rentention
* @Target
* @Inhrited
* @Documented
1.內置注解
@Deperated
用來標記一個方法,類,或者屬性已經過期
@Override
用來標記一個方法是重載父類的, 會到他的上層或者上上層去尋找, 直到找到位置
@SupressWarning
源碼:
@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.CONSTRUCTOR, ElementType.LOCAL_VARIABLE})
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
String[] value();
}
讓編譯器忽略一些提醒,比如過時的方法等
2.元注解
用來標注注解類的注解,可以用于自定義注解
@Retention
注解什么時候可見, 分為(運行時可見,.class 及源碼中可見 , 源碼可見)
源碼:
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.ANNOTATION_TYPE})
public @interface Retention {
RetentionPolicy value();
}
添加注解時的參數類型為RetentionPolicy
的
public enum RetentionPolicy {
CLASS, //.class 或者源碼中可見
RUNTIME, //運行時可見,可以通過反射獲得
SOURCE; //僅在源碼中可見
private RetentionPolicy() {
}
}
@Target
用于標記該注解適用于什么類型的元素(類,方法,變量等)
添加注解時的參數類型是ElementType
的
public enum ElementType {
ANNOTATION_TYPE, //用于注解其他注解
CONSTRUCTOR,
FIELD,
LOCAL_VARIABLE,
METHOD,
PACKAGE, //該注解用于注解包聲明
PARAMETER,
TYPE; //標示該注解用于注解類,接口,枚舉類型
private ElementType() {
}
}
@Inhrited
表明被它修飾的注解具有繼承性
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface InheritedAnnotation {
}
class A{
@InheritedAnnotion
classB{}
classC extends ClassB{}
public void static print(){
system.out.println(ClassC.class.isAnnotationPresent(InheritedAnnotation.class));
}
}
最終 print
方法輸出結果為true
@Documented
直接在需要加入到JavaDoc的地方加上該注解即可