註解


瞭解註解及java提供的幾個基本註解

如果某些方法已經過時了,但是你仍然想使用它,又不想編譯器提出警告可以使用@SuppressWarnings"deprecation"

如果你想告訴別人某個方法已經過時了,想讓後人減少對該方法的使用可以用@Deprecated

當你要對某個方法進行重寫時,你想不出差錯,應該使用@Override

總結:

 註解相當於一種標記,加了註解就等於打上了某種標記,沒加,則等於沒有某種標記,以後,javac編譯器,開發工具和其他程序可以用反射來了解你的類及各種元素上有無何種標記,看你有什麼標記,就去幹相應的事。標記可以加在包,類,字段,方法,方法的參數以及局部變量上。

java.lang包,可以看到JDK中提供的最基本得annotation

註解的應用結構

1. 註解類

    @interface A{}

2. 應用了註解類的類

@A

Class B{}

3. 對“應用了註解類的類”進行反射操作的類

Class C

{

    B.class.isAnnotionPresent(A.class);

    A a = B.class.getAnnotion(A.class);

}

下面的代碼展示了最簡單得這種結構:

public @interface ItcastAnnotation {  }
------------------------------------------------------
@ItcastAnnotation
public class AnnotationTest {
     public static void main(String[] args) throws Exception{
           	//判斷當前類裏面有沒有ItcastAnnotation這個註解
		if(AnnotationTest.class.isAnnotationPresent(ItcastAnnotation.class)){
			//得到註解類
			ItcastAnnotation annotation = 
      (ItcastAnnotation)AnnotationTest.class.getAnnotation(ItcastAnnotation.class);
		System.out.println(annotation);
       }
	}
}

如果就這樣打印在控制檯,將看不到任何結果,因爲jvm在加載類文件進入內存時,會去掉註解信息,此時你得指定註解要保留到什麼時候

這個時候就要用到@Retetnion元註解,它有三種取值:

  • 這個就是註解的生命週期Enum RetentionPolicy
  • RetentionPolicy.SOURCE------>java源文件
  • RetentionPolicy.CLASS------->class文件
  • RetentionPolicy.RUNTIME---->內存中的字節碼

那麼在我們的註解類前面再加上這個註解就行了(註解的註解--元註解)

@Retention(RetentionPolicy.RUNTIME)//一直保留在運行期間

public @interface ItcastAnnotation { }

@Override ------>RetentionPolicy.SOURCE

@SuppressWarnings----->RetentionPolicy.SOURCE

@Deprecated---->RetentionPolicy.RUNTIME

註解加在類的什麼位置可以用@Target元註解

   Target的默認值爲任何元素,設置Target等於ElementType.METHOD原來加在類上面的註解就報錯了,改爲用數組方式設置{ElementType.METHOD,ElementType.TYPE}就可以了。


可以爲註解增加基本屬性

定義基本類型的屬性和應用屬性

     在註解類中增加String color()

    @MyAnnotation(color ="red")

用反射方式獲得註解對應的實例對象後,再通過該對象調用屬性對應的方法

       MyAnonotation a =  (MyAnnotation)AnnotationTest.calss.getAnnotation(MyAnonotation.class)

       System.out.println(a.color())

       可以認爲上面這個@MyAnnotation是MyAnnotation類的一個實例對象

爲屬性指定缺省值

      String color()default "yellow";

value屬性:

 String value() default "zxx";

 如果註解中有一個名稱爲value的屬性,且你只想設置value屬性(即其他屬性都採用默認值或者你只有一個value屬性),那麼可以省略value=部分,例如@MyAnnotation("lhm")

爲註解增加高級屬性

  數組類型的屬性

   int[] arrayArr() default {1,2,3};

   @MyAnnotation(arrayAttr={2,3,4}) 

   如果數組屬性中只有一個元素,這時候屬性值部分可以省略大括號

 枚舉類型的屬性

   EnumTest.TrafficLamp lamp();

   @MyAnnotation(lamp=EnumTest.TrafficLamp.GREEN)

 註解類型的屬性

   MetaAnnotation annotationAttr() default @MetaAnnotation("xxx");

   @MyAnnotation(annotationAttr= @MetaAnnotation("yyy"))

   可以認爲上面這個@MyAnnotation是MyAnnotation類的一個實例對象,同樣的道理,可以認爲上面這個@MetaAnnotation是MetaAnnotation類的一個實例對象,調用代碼如下:

   MetaAnnotation ma = myAnnotation.anntationAttr();

   System.out.println(ma.value());

註解的詳細語法可以通過看java語言規範瞭解,即看java的language specification。

註解的代碼如下:

public @interface MetaAnnotation {
	String value();
}
--------------------------------------------------------
@Retention(RetentionPolicy.RUNTIME)//一直保留在運行期間
@Target({ElementType.METHOD,ElementType.TYPE})
//指明可以添加到類前面和方法的前面
public @interface ItcastAnnotation {
	String color() default "blue";
	String value();
	//數組類型
	int[] arrayAttr() default {3,4,4};
	//枚舉類型的
	EnumTest.TrafficLamp lamp() default EnumTest.TrafficLamp.RED;
	//註解類型的屬性
	MetaAnnotation annotationAttr() default @MetaAnnotation("lhm");
}
---------------------------------------------------------------------------------
@ItcastAnnotation(annotationAttr=@MetaAnnotation("flx"),color="red",value="abc",arrayAttr=1)
//對於數組arrayAttr 可以這樣賦值arrayAttr={1,2,3},如果數組中只有一個元素arrayAttr={1},也可以寫成arrayAttr=1
public class AnnotationTest {

	/**
	 * @param args
	 */
	@SuppressWarnings("deprecation")//不提示過時
	@ItcastAnnotation("xyz")
	public static void main(String[] args) throws Exception{
			//判斷當前類裏面有沒有ItcastAnnotation這個註解
		if(AnnotationTest.class.isAnnotationPresent(ItcastAnnotation.class)){
			//得到註解類
			ItcastAnnotation annotation = 
       (ItcastAnnotation)AnnotationTest.class.getAnnotation(ItcastAnnotation.class);
			System.out.println(annotation.color());
			System.out.println(annotation.value()); 
			System.out.println(annotation.arrayAttr().length);
			System.out.println(annotation.lamp().nextLamp().name());
			System.out.println(annotation.annotationAttr().value());
		}



發佈了25 篇原創文章 · 獲贊 21 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章