1.概述
注解可以定義到方法上,類上,一個注解相當與一個類,就相當于實例了一個對象,加上了注解,就相當于加了一個標志。
常用的注解:
@Override:表示重新父類的方法,
這個也可以判斷是否覆蓋的父類方法,在方法前面加上此語句,如果提示的錯誤,那么你不是覆蓋的父類的方法,要是提示的沒有錯誤,那么就是覆蓋的父類的方法。
@SuppressWarnings("deprecation"):取消編譯器的警告(例如你使用的方法過時了)
@Deprecated:在方法的最上邊也上此語句,表示此方法過時,了,或者使用在類上面
import java.util.ArrayList;
import java.util.List;
public class annotationDemo {
/*
* 對于集合,如果沒有指定存儲的類型,那么就會有安全警告,
* 如果不想提示安全警告的話,那么就所在類或者方法上添加@SuppressWarnings(參數)
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) {
List list=new ArrayList();
}
}
2.自定義注解
1.格式
權限 @interface 注解名稱 { }
步驟:
定義注解類--->定義應用注解類的類--->對應用注解類的類進行反射的類(這個類可以另外定義,也可以是在應用注解類中進行測試)
import java.lang.annotation.Retention;
importjava.lang.annotation.RetentionPolicy;
//定義此注解保留在字節碼中
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
}
@MyAnnotation
// 應用定義的注解類
public class ApplyMyAnnotation {
public static void main(String[] args) {
if (ApplyMyAnnotation.class.isAnnotationPresent(MyAnnotation.class)) {// 判斷此類上是否存在指定的注解類
MyAnnotation annotation= (MyAnnotation) ApplyMyAnnotation.class
.getAnnotation(MyAnnotation.class);
System.out.println(annotation);
}
}
}
2.聲明周期
格式:例如:@Retention(RetentionPolicy.CLASS)
在自定一的注解類上定義周期,@Retention(參數類型) 參數類型是RetentionPolicy
RetentionPolicy.CLASS:類文件上,運行時虛擬機不保留注解
RetentionPolicy.RUNTIME:類文件上,運行時虛擬就保留注解
RetentionPolicy.SOURCE:源文件上,丟棄注解
SuppressWarnings和Override是RetentionPolicy.SOURCE,
Deprecated是在RetentionPolicy.RUNTIME,要向運行時調用定義的一樣,那么必須是RetentionPolicy.RUNTIME,
默認的都是RetentionPolicy.CLASS:
3.指定目標
格式:例如:方法上@Target(ElementType.METHOD)
定義的注解可以注解什么成員。如果不聲明此注解,那么就是可以放到任何程序的元素上。
可以是包,接口,參數,方法,局部變量,字段…等。
//定義此注解保留在字節碼中
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})//可以定義在方法上和類上接口,表示類型
public @interface MyAnnotation {
}
@MyAnnotation
// 應用定義的注解類
public class ApplyMyAnnotation {
@MyAnnotation//定義在方法上
public static void main(String[] args) {
if (ApplyMyAnnotation.class.isAnnotationPresent(MyAnnotation.class)) {// 判斷此類上是否存在指定的注解類
MyAnnotation annotation = (MyAnnotation) ApplyMyAnnotation.class
.getAnnotation(MyAnnotation.class);
System.out.println(annotation);
}
}
}
3.為注解添加屬性
1.類型
注解的屬性置可以是:8個基本數據類型,String,枚舉,注解,Class,數組類型,
2.注意點
當注 解中只有一個屬性或者是只有一個屬性需要賦值的話,那么在調用的時候,就可以直接寫入,不需要指定屬性名,
當注解的屬性是數組類型并且賦值的時候只賦值一個值,那么就可以省略{}.
3.示例
3.1.屬性類型(是String)
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.*;
//定義此注解保留在字節碼中
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value() ;
String Color()default "red";//設置默認值是"red"
}
@MyAnnotation("java")
public class ApplyMyAnnotation {
public static void main(String[] args) {
/**
* 這是獲得類上的注解,也可以獲得方法上的注解,下面就以獲得類上的注解為例
*/
if (ApplyMyAnnotation.class.isAnnotationPresent(MyAnnotation.class)) {// 判斷此類上是否存在指定的注解類
MyAnnotation annotation = (MyAnnotation) ApplyMyAnnotation.class
.getAnnotation(MyAnnotation.class);
System.out.println("value="+annotation.value());
System.out.println("Color="+annotation.Color());
}
}
}
結果:
value=java
Color=red
從調用的程序中,也可以看出,只有一個屬性可以需要賦值的話,可以省略屬性名。否則@注解類(屬性名=值)
3.2.綜合類型
/*枚舉類*/
public enum Week{
SUN,MON;
}
/**
* 注解類
*/
public @interface annotationText {
String value();
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.*;
//定義此注解保留在字節碼中
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value() ;
String Color()default "red";//設置默認值是"red"
Week week() default Week.MON;//枚舉類型
int [] array() default {1,2,3};//數組類型
annotationText annotation() default @annotationText("MY");//注解類型
Class classDemo() default Integer.class;//Class類型
}
@MyAnnotation(value="java",Color="green",week=Week.SUN,array=5,annotation=@annotationText("YOU"),classDemo=String.class)//數組array={4,5,6}
public class ApplyMyAnnotation {
public static void main(String[] args) {
/**
* 這是獲得類上的注解,也可以獲得方法上的注解,下面就以獲得類上的注解為例
*/
if (ApplyMyAnnotation.class.isAnnotationPresent(MyAnnotation.class)) {// 判斷此類上是否存在指定的注解類
MyAnnotation annotation= (MyAnnotation) ApplyMyAnnotation.class
.getAnnotation(MyAnnotation.class);
System.out.println("value="+annotation.value());
System.out.println("Color="+annotation.Color());
System.out.println("week="+annotation.week());
System.out.println("array長度="+annotation.array()。length);
System.out.println("注解類型值="+annotation.annotation()。value());
System.out.println("Class類型值="+annotation.classDemo());
}
}
}
結果:
value=java
Color=green
week=SUN
array長度=1
注解類型值=YOU
Class類型值=classjava.lang.String
4.Method上的注解
importjava.lang.annotation.Retention;
importjava.lang.annotation.RetentionPolicy;
/**
*注解類
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface annotationText{
Stringvalue();
}
publicclassApplyMyAnnotation{
publicstaticvoidmain(String[]args)throwsException{
Methodmethodshow=ApplyMyAnnotation.class.getMethod("show");
annotationTextanno=methodshow.getAnnotation(annotationText.class);
System.out.println(anno.value());
}
@annotationText("java")
publicvoidshow(){
System.out.println("hello");
}
}
結果:java