乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      張龍 Annotation學(xué)習(xí)筆記

       狼志凌云 2011-10-08

      首先明確一個(gè)概念:
      annotation=注解      comment=注釋  不要混淆了

      a) Override注解表示子類要重寫(override)父類的對(duì)應(yīng)方法。
      舉例:OverrideTest.java

       

      Java代碼 復(fù)制代碼 收藏代碼
      1. package com.shengsiyuan.annotation;   
      2.   
      3. public class OverrideTest   
      4. {   
      5.     @Override  
      6.         //該注解表示該類重寫了父類的方法   
      7.     public String toString()   
      8.     {   
      9.         return "This is OverrideTest";   
      10.     }   
      11.        
      12.     public static void main(String[] args)   
      13.     {   
      14.         OverrideTest test = new OverrideTest();   
      15.            
      16.         System.out.println(test);   
      17.     }   
      18. }   

      b) Deprecated注解表示方法是不建議被使用的。
      舉例:DeprecatedTest.java 

      Java代碼 復(fù)制代碼 收藏代碼
      1. package com.shengsiyuan.annotation;   
      2.   
      3. public class DeprecatedTest   
      4. {   
      5.     @Deprecated  
      6.         //該注解表示這個(gè)方法是廢棄的,不建議被使用的   
      7.     public void doSomething()   
      8.     {   
      9.         System.out.println("do something!");   
      10.     }   
      11.        
      12.     public static void main(String[] args)   
      13.     {   
      14.         DeprecatedTest test = new DeprecatedTest();   
      15.            
      16.         test.doSomething();   
      17.            
      18.            
      19.            
      20.            
      21.            
      22.            
      23. //      Date date = new Date();   
      24. //         
      25. //      System.out.println(date.toLocaleString());   
      26.     }   
      27. }  

      c) SuppressWarnings注解表示抑制警告。
      舉例:SuppressWarningsTest.java

      Java代碼 復(fù)制代碼 收藏代碼
      1. package com.shengsiyuan.annotation;   
      2.   
      3. import java.util.Date;   
      4. import java.util.Map;   
      5. import java.util.TreeMap;   
      6.   
      7. public class SuppressWarningsTest   
      8. {   
      9.     @SuppressWarnings({"unchecked""deprecation"})   
      10.    //該注解表示一些警告會(huì)被壓制住   
      11.    //unchecked表示不對(duì)集合沒有使用泛型進(jìn)行警告   
      12.    //deprecation表示不對(duì)已經(jīng)廢棄的方法的使用進(jìn)行警告   
      13.     public static void main(String[] args)   
      14.     {   
      15.         Map map = new TreeMap();   
      16.            
      17.         map.put("hello"new Date());   
      18.            
      19.         System.out.println(map.get("hello"));   
      20.            
      21.         Date date = new Date();   
      22.            
      23.         System.out.println(date.toLocaleString());   
      24.     }   
      25. }  

      關(guān)于自定義注解

      自定義注解:當(dāng)注解中的屬性名為value時(shí),在對(duì)其賦值時(shí)可以不指定屬性的名稱而直接寫上屬性值即可;除了value以外的其他值都需要使用name=value這種賦值方式,即明確指定給誰賦值。
      舉例如下:
      (1)先定義一個(gè)注解 

      Java代碼 復(fù)制代碼 收藏代碼
      1. package com.shengsiyuan.annotation;   
      2.   
      3. public @interface AnnotationTest   
      4. {   
      5.         //注解里定義屬性要在屬性后面加一個(gè)括號(hào)   
      6.     String[] value1() default "hello";//有一個(gè)默認(rèn)值hello   
      7.     EnumTest value2(); //注解里面的值也可定義為枚舉類型   
      8. }   
      9.   
      10. enum EnumTest   
      11. {   
      12.     Hello, World, Welcome;   
      13. }  

       注意:當(dāng)一個(gè)接口繼承Annotation接口時(shí),該接口依然只是一個(gè)接口,還不是注解類型,
               要定義注解類型只有一種方式:通過@interface關(guān)鍵字,除此之外別無他法。
               另外,Annotation本身也只是一個(gè)接口,并不是注解類型(可以查看API文檔有        Annotation這個(gè)接口)

      (2)引用上面的注解 

      Java代碼 復(fù)制代碼 收藏代碼
      1. package com.shengsiyuan.annotation;   
      2.   
      3. @AnnotationTest(value2 = EnumTest.Welcome)   
      4. public class AnnotationUsage   
      5. {   
      6.     @AnnotationTest(value1 = {"world""ABCD"}, value2 = EnumTest.World)   
      7.     public void method()   
      8.     {   
      9.        System.out.println("usage of annotation");   
      10.     }   
      11.        
      12.     public static void main(String[] args)   
      13.     {   
      14.        AnnotationUsage usage = new AnnotationUsage();   
      15.            
      16.                    usage.method();   
      17.     }   
      18. }  

       

      最后有一點(diǎn)需要注意的是:
      當(dāng)我們使用@interface關(guān)鍵字定義一個(gè)注解時(shí),該注解隱含地繼承了java.lang.annotation.Annotation接口;如果我們定義了一個(gè)接口,并且讓該接口繼承自Annotation,那么我們所定義的接口依然還是接口而不是注解;Annotation本身是接口而不是注解??梢耘cEnum類比。
       
       
       
      第2集
       

      本集主要講述@Retention及@Target2個(gè)注解,順帶提一下@Documented這個(gè)注解

       

      1.關(guān)于@Retention這個(gè)注解

      Retention翻譯成中文是“保留”的意思,RetentionPolicy是“保留策略”。

      簡要描述:指示注解類型的注解要保留多久。如果注解類型聲明中不存在 Retention 注解,則保留策略默認(rèn)為  RetentionPolicy.CLASS

      每一個(gè)Retention都要給他一個(gè)RetentionType,RetentionType是一個(gè)枚舉類型(具體可以查看API文檔),它有3種取值:SOURCE,CLASS,RUNTIME,區(qū)別如下:

      (a)SOURCE:表示該注解只會(huì)存在于JAVA源文件中,不會(huì)編譯到class文件里面去,更不會(huì)在運(yùn)行期通過反射的方式獲   取到。

      (b)CLASS:表示該注解會(huì)隨著JAVA源代碼一起編譯到class文件里面去,但不會(huì)在運(yùn)行期通過反射的方式獲取到。

      (c)RUNTIME:表示該注解會(huì)隨著JAVA源代碼一起編譯到class文件里面去,并且會(huì)在運(yùn)行期通過反射的方式獲取到。

       

      請看一個(gè)示例:

      首先定義一個(gè)注解:

      Java代碼 復(fù)制代碼 收藏代碼
      1. package com.shengsiyuan.annotation;   
      2.   
      3. import java.lang.annotation.Retention;   
      4. import java.lang.annotation.RetentionPolicy;   
      5.   
      6.   
      7. //注解也可以修飾注解,該注解修飾下面自定義的注解,通過設(shè)定   
      8. //RetentionPolicy的值為RUNTIME表示該自定義的注解會(huì)被編   
      9. //譯到CLASS文件當(dāng)中,而且可以在運(yùn)行期通過反射的方式獲取到(具體請查看一遍API文檔,很有必要!)   
      10. @Retention(RetentionPolicy.RUNTIME)   
      11. public @interface MyAnnotation   
      12. {   
      13.     String hello() default "shengsiyuan";   
      14.   
      15.     String world();   
      16. }  

      然后定義一個(gè)類,用這個(gè)Annotation去修飾

      Java代碼 復(fù)制代碼 收藏代碼
      1. package com.shengsiyuan.annotation;   
      2.   
      3. @MyAnnotation(hello = "beijing", world = "shanghai")   
      4. public class MyTest   
      5. {          
      6.         //一個(gè)方法可以被多個(gè)注解所修飾。   
      7.     @MyAnnotation(hello = "tianjin", world = "shangdi")   
      8.     @Deprecated  
      9.     @SuppressWarnings("unchecked")    
      10.     public void output()   
      11.     {   
      12.         System.out.println("output something!");   
      13.     }   
      14. }  

       接著定義一個(gè)類,并通過反射相關(guān)API去獲得自定義注解的相關(guān)信息

      Java代碼 復(fù)制代碼 收藏代碼
      1. package com.shengsiyuan.annotation;   
      2.   
      3. import java.lang.annotation.Annotation;   
      4. import java.lang.reflect.Method;   
      5.   
      6. //該類拿到修飾MyTest里方法的Annotation   
      7. public class MyReflection   
      8. {   
      9.     public static void main(String[] args) throws Exception   
      10.     {   
      11.         MyTest myTest = new MyTest();   
      12.            
      13.         Class<MyTest> c = MyTest.class;   
      14.            
      15.         Method method = c.getMethod("output"new Class[]{});   
      16.            
      17.                 //能夠進(jìn)入到if語句里面來說明MyAnnotation的RetentionPolicy的值為Runtime(為什么請查API文檔!)   
      18.         if(method.isAnnotationPresent(MyAnnotation.class))   
      19.         {   
      20.             method.invoke(myTest, new Object[]{});   
      21.                
      22.             MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class);   
      23.                
      24.             String hello = myAnnotation.hello();   
      25.             String world = myAnnotation.world();   
      26.                
      27.             System.out.println(hello + ", " + world);   
      28.         }   
      29.   
      30.         //只會(huì)得到Myannotation和Deprecated兩個(gè)Annotation,因?yàn)橹挥羞@兩個(gè)Annotation的RetentionPolicy   
      31.                 //的值為Runtime,只有RetentionPolicy的值為Runtime才會(huì)在運(yùn)行期通過反射相關(guān)API拿到Annotation的相關(guān)信息。   
      32.         Annotation[] annotations = method.getAnnotations();   
      33.            
      34.         for(Annotation annotation : annotations)   
      35.         {   
      36.             System.out.println(annotation.annotationType().getName());   
      37.         }   
      38.     }   
      39. }  

       

      2.關(guān)于@Target這個(gè)注解(建議去讀一讀API文檔,介紹的很詳細(xì))

       簡要描述:指示注解類型所適用的程序元素的種類。如果注解類型聲明中不存在 Target 元注解,則聲明的類型可以用在任一程序元素上。

      每一個(gè)Target都要給他一個(gè)ElementType,ElementType是一個(gè)枚舉類型(具體可以查看API文檔),它有8種取值:SOURCE,CLASS,RUNTIME,區(qū)別如下:

      (a)ANNOTATION_TYPE:表示該注解可以去修飾另外一個(gè)注解

      (b)COUNSTRUCTOR:表示該注解可以修飾構(gòu)造方法

      (c)FIELD:表示該注解可以修飾成員變量

      (d)LOCAL_VARIABLE:表示該注解可以修飾局部變量

      (e)METHOD:表示該注解可以修飾普通方法

      (f)PACKAGE:表示該注解可以修飾包

      (g)PARAMETER:表示該注解可以修飾方法參數(shù)

      (h)TYPE:表示該注解可以修飾類、接口(包括注解類型)或枚舉聲明

       

      請看示例:

      Java代碼 復(fù)制代碼 收藏代碼
      1. package com.shengsiyuan.annotation;   
      2.   
      3. import java.lang.annotation.ElementType;   
      4. import java.lang.annotation.Target;   
      5.   
      6. @Target(ElementType.METHOD)//表示該自定義注解只能用于修飾方法   
      7. public @interface MyTarget   
      8. {   
      9.     String value();   
      10. }  

      接著定義一個(gè)類:

      Java代碼 復(fù)制代碼 收藏代碼
      1. package com.shengsiyuan.annotation;   
      2.   
      3. public class MyTargetTest   
      4. {   
      5.     @MyTarget("hello")   
      6.     public void doSomething()   
      7.     {   
      8.         System.out.println("hello world");   
      9.     }   
      10. }  

       當(dāng)把該自定義的注解放到方法上面后編譯器不報(bào)錯(cuò)時(shí),說明我們的實(shí)驗(yàn)是成功的(不需要寫main方法進(jìn)行測試)

       

      對(duì)以上2個(gè)注解的總結(jié):Retention與Target都是注解,Retention與RetentionPolicy搭配,Target與ElementType搭配。

       

      3.關(guān)于@Documented(了解就行)

      不多做描述,請看示例:

      Java代碼 復(fù)制代碼 收藏代碼
      1. package com.shengsiyuan.annotation;   
      2.   
      3. import java.lang.annotation.Documented;   
      4.   
      5. @Documented   //當(dāng)一個(gè)注解被@Documented 修飾后表示被該注解修飾的對(duì)象(類或方法或其他)在生成JAVA DOC文檔時(shí),該注解會(huì)被加到修飾的對(duì)象的上面   
      6. public @interface DocumentedAnnotation   
      7. {   
      8.     String hello();   
      9. }  

       然后用該注解去修飾某個(gè)方法

      Java代碼 復(fù)制代碼 收藏代碼
      1. package com.shengsiyuan.annotation;   
      2.   
      3. public class DocumentedTest   
      4. {   
      5.     @DocumentedAnnotation(hello = "welcome")   
      6.     public void method()   
      7.     {   
      8.         System.out.println("hello world");   
      9.     }   
      10. }  

       當(dāng)對(duì)DocumentedTest所在的包或工程生成JAVA DOC文檔的時(shí)候,會(huì)發(fā)現(xiàn)自定義的注解會(huì)出現(xiàn)在method方法的上面

       

       

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請遵守用戶 評(píng)論公約

        類似文章 更多