欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
JAVA annotation
              JAVA annotation入門(mén)

一. 最常見(jiàn)的annotation
  • @Override:用在方法之上,用來(lái)告訴別人這一個(gè)方法是改寫(xiě)父類(lèi)的
  • @Deprecated:建議別人不要使用舊的API的時(shí)候用的,編譯的時(shí)候會(huì )用產(chǎn)生警告信息,可以設定在程序里的所有的元素上.
  • @SuppressWarnings:暫時(shí)把一些警告信息消息關(guān)閉
  • @Entity:表示該類(lèi)是可持久化的類(lèi)
 
二. 設計一個(gè)自己的Annotation
      先看代碼再講話(huà)
1. 只有一個(gè)參數的Annotation實(shí)現

  1. package chb.test.annotation;  
  2. import java.lang.annotation.Documented;  
  3. import java.lang.annotation.ElementType;  
  4. import java.lang.annotation.Retention;  
  5. import java.lang.annotation.RetentionPolicy;  
  6. import java.lang.annotation.Target;  
  7. @Target(ElementType.TYPE)  
  8. @Retention(RetentionPolicy.RUNTIME)  
  9. @Documented  
  10. public @interface MyAnnotation1 {  
  11.         String value();  
  12. }  
 
 
2. 有兩個(gè)參數的Annotation實(shí)現

  1. package chb.test.annotation;  
  2. import java.lang.annotation.Documented;  
  3. import java.lang.annotation.ElementType;  
  4. import java.lang.annotation.Retention;  
  5. import java.lang.annotation.RetentionPolicy;  
  6. import java.lang.annotation.Target;  
  7. @Target(ElementType.METHOD)  
  8. @Retention(RetentionPolicy.RUNTIME)  
  9. @Documented  
  10. public @interface MyAnnotation2 {  
  11.         String description();  
  12.         boolean isAnnotation();  
  13. }  
 
 
3. Annotation實(shí)驗類(lèi)

  1. package chb.test.annotation;  
  2. @MyAnnotation1("this is annotation1")  
  3. public class AnnotationDemo {  
  4.         @MyAnnotation2(description="this is annotation2",isAnnotation=true)  
  5.         public void sayHello(){  
  6.                 System.out.println("hello world!");  
  7.         }  
  8. }  
 
 
4.Annotation測試說(shuō)明類(lèi)
 
  1. package chb.test.annotation;  
  2. import java.lang.reflect.Method;  
  3. import org.junit.Test;  
  4. public class TestAnnotation {  
  5.         @Test  
  6.         public void test() throws ClassNotFoundException, SecurityException, NoSuchMethodException{  
  7.                 Class<?> cls = Class.forName("chb.test.annotation.AnnotationDemo");  
  8.                 boolean flag = cls.isAnnotationPresent(MyAnnotation1.class);  
  9.                 if(flag){  
  10.                         System.out.println("判斷類(lèi)是annotation");  
  11.                         MyAnnotation1 annotation1 = cls.getAnnotation(MyAnnotation1.class);  
  12.                         System.out.println(annotation1.value());  
  13.                 }  
  14.                   
  15.                 Method method = cls.getMethod("sayHello");  
  16.                 flag = method.isAnnotationPresent(MyAnnotation2.class) ;  
  17.                 if(flag){  
  18.                         System.out.println("判斷方法也是annotation");  
  19.                         MyAnnotation2 annotation2 = method.getAnnotation(MyAnnotation2.class);  
  20.                         System.out.println(annotation2.description()+"\t"+annotation2.isAnnotation());  
  21.                 }  
  22.         }  
  23.           
  24. }  
 
 
實(shí)驗結果,控制臺打出如下信息:
 
判斷類(lèi)是annotation
this is annotation1
判斷方法也是annotation
this is annotation2     true
 
三.簡(jiǎn)介及說(shuō)明
 
1. MyAnnotation1中的@Target(ElementType.TYPE)
      @Target里面的ElementType是用來(lái)指定Annotation類(lèi)型可以用在哪些元素上的.例如:
       TYPE(類(lèi)型)、FIELD(屬性)、METHOD(方法)、PARAMETER(參數)、CONSTRUCTOR(構造函數)、LOCAL_VARIABLE(局部變量),、PACKAGE(包),其中的TYPE(類(lèi)型)是指可以用在Class,Interface,Enum和Annotation類(lèi)型上。
2. MyAnnotation1中的@Retention(RetentionPolicy.RUNTIME)
      RetentionPolicy 共有三種策略,分別為:
  • SOURCE:這個(gè)Annotation類(lèi)型的信息只會(huì )保留在程序源碼里,源碼如果經(jīng)過(guò)了編譯之后,Annotation的數據就會(huì )消失,并不會(huì )保留在編譯好的.class文件里面
  • CLASS:這個(gè)Annotation類(lèi)型的信息保留在程序源碼里,同時(shí)也會(huì )保留在編譯好的.class文件里面,在執行的時(shí)候,并不會(huì )把這些信息加載到JVM中。注:默認策略為CLASS類(lèi)型
  • RUNTIME:表示在源碼、編譯好的.class文件中保留信息,在執行的時(shí)候會(huì )把這一些信息加載到JVM中去的
3. MyAnnotation1中的@Documented
目的就是將這一Annotation的信息顯示在JAVA API文檔上,如果沒(méi)有增加@Documented的話(huà),JAVA API文檔上不會(huì )顯示相關(guān)annotation信息
 
4. MyAnnotation1中的@interface
   關(guān)鍵字,表示該類(lèi)為Annotation定義
5. MyAnnotation1中的 String value();
   表示有一個(gè)成員參數,名字為value,訪(fǎng)問(wèn)權為默認(default)修飾符,注意以下兩點(diǎn):
  • 訪(fǎng)問(wèn)權只能用public和默認(default)修飾
  • 參數成員只能用基本類(lèi)型byte,short,char,int,long,float,double,boolean八種基本數據類(lèi)型和String,Enum,Class,annotations等數據類(lèi)型,以及這一些類(lèi)型的數組
6.AnnotationDemo中的@MyAnnotation1("this is annotation1")
   因為MyAnnotation1只有一個(gè)參數,因此可以直接在括號中寫(xiě)上value值。注:如果Annotation只有一個(gè)參數,則建議最好將該參數名稱(chēng)定義為value
7.TestAnnotation中的cls.isAnnotationPresent(MyAnnotation1.class)
    判斷該類(lèi)是否使用了MyAnnotation1的注釋
8. TestAnnotation中的MyAnnotation1 annotation1 = cls.getAnnotation(MyAnnotation1.class)
    返回該類(lèi)針對MyAnnotation1的注釋
9. TestAnnotation中的method.isAnnotationPresent(MyAnnotation2.class)
    判斷該方法是否使用了MyAnnotation2的注釋

本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
張龍 Annotation學(xué)習筆記
Java Annotation之介紹篇 - 開(kāi)心就好! - JavaEye技術(shù)網(wǎng)站
java.lang和java.lang.annotation中實(shí)現Annotation的類(lèi)小結
annotation 的學(xué)習
java annotation(注解)
Java自定義注解
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久