Loading...

          java .net

          自定義Annotation

          自定義Annotation
          早就知道jdk5加了新特性Annotation,但是沒(méi)研究過(guò),前幾天公司培訓(xùn),有一部分是介紹jdk5新特性的,一個(gè)是注解一個(gè)泛型
          今兒復(fù)習(xí)一下注解
              //用@Deprecated聲明該方法不建議使用
              @Deprecated public void doSomething1(){
                  Map map = new HashMap();
                  map.put("some", "thing");
                  System.out.println(map);
              }
             
              //用@SuppressWarnings聲明不再進(jìn)行類型檢查
              @SuppressWarnings(value={"unchecked"})
              public void doSomething2(){
                  Map map = new HashMap();
                  map.put("some", "thing");
              }


          寫一個(gè)自定義注解先
          import java.lang.annotation.Retention;
          import java.lang.annotation.RetentionPolicy;
          //要在運(yùn)行時(shí)使用這個(gè)注解,必須聲明成RUNTIME
          Annotation分為三種級(jí)別:RUNTIME、CLASS、SOURCE
          @Retention(RetentionPolicy.RUNTIME)
          public @interface SomeAnnotation{
              String value();
              String name();
          }

          下面來(lái)使用這個(gè)自定義注解:
          import java.lang.reflect.Method;
          public class AnnotationTest {
             
              @SomeAnnotation(value="value1",name="name1")
              public void doSomething3(){
                 
              }
             
              public static void main(String[] args){
             
                  Class<AnnotationTest> c = AnnotationTest.class;
                  try {
                      //利用反射得到方法doSomething3
                      Method method = c.getMethod("doSomething3");
                      //查找doSomething3方法是否有SomeAnnotation的Annotation
                      if(method.isAnnotationPresent(SomeAnnotation.class)){
                          System.out.println("找到SomeAnnotation");
                          //得到SomeAnnotation
                          SomeAnnotation annotation = method.getAnnotation(SomeAnnotation.class);
                          System.out.println("annotation.value="+annotation.value());
                          System.out.println("annotation.name="+annotation.name());
                      }else{
                          System.out.println("沒(méi)有找到omeAnnotation");
                      }
                  } catch (SecurityException e) {
                      e.printStackTrace();
                  } catch (NoSuchMethodException e) {
                      e.printStackTrace();
                  }
              }
          }

          輸出結(jié)果:
          找到SomeAnnotation
          annotation.value=value1
          annotation.name=name1

          posted on 2008-12-28 16:47 閱讀(3835) 評(píng)論(6)  編輯  收藏 所屬分類: 學(xué)習(xí)java

          評(píng)論

          # re: 自定義Annotation 2008-12-28 19:05 劍網(wǎng)3論壇

          學(xué)習(xí)咯~~~~  回復(fù)  更多評(píng)論   

          # re: 自定義Annotation 2008-12-28 21:53 ttt

          http://www.cubebi.com  回復(fù)  更多評(píng)論   

          # re: 自定義Annotation 2008-12-29 08:28 日月雨林@gmail.com

          這個(gè)是我最容易明白的一篇文章了!太感謝了!  回復(fù)  更多評(píng)論   

          # re: 自定義Annotation 2008-12-29 13:03

          (*^__^*) 嘻嘻……   回復(fù)  更多評(píng)論   

          # re: 自定義Annotation 2008-12-29 20:24 Strive

          頂一個(gè)先。。  回復(fù)  更多評(píng)論   

          # re: 自定義Annotation 2009-01-08 14:49 kangsg219

          學(xué)習(xí)了,謝謝  回復(fù)  更多評(píng)論   

          公告

          希望有一天

          我能用鼠標(biāo)雙擊我的錢包

          然后選中一張100元

          按住“ctrl+c”

          接著不停的“ctrl+v”

          嘻嘻~~~笑醒~~~



          導(dǎo)航

          <2008年12月>
          30123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          統(tǒng)計(jì)

          常用鏈接

          留言簿(6)

          隨筆分類(102)

          隨筆檔案(398)

          文章分類

          文章檔案(10)

          有趣網(wǎng)絡(luò)

          搜索

          積分與排名

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 喀喇| 瑞安市| 吴江市| 洛阳市| 西乌| 宣化县| 扎鲁特旗| 十堰市| 汉川市| 砀山县| 中阳县| 本溪市| 台南市| 封丘县| 上栗县| 阳原县| 远安县| 阿荣旗| 沂源县| 札达县| 秦皇岛市| 弥渡县| 长治县| 武宁县| 梁山县| 囊谦县| 南涧| 富锦市| 东港市| 偏关县| 盖州市| 依安县| 延吉市| 绥滨县| 克什克腾旗| 逊克县| 班玛县| 大邑县| 巴林左旗| 北京市| 辰溪县|