posts - 431,  comments - 344,  trackbacks - 0

          編寫Annotation:

          package com.eric.news.cha06;

          import Java.lang.annotation.Documented;
          import Java.lang.annotation.Inherited;
          import Java.lang.annotation.Retention;
          import Java.lang.annotation.RetentionPolicy;

          @Documented
          @Inherited
          @Retention(RetentionPolicy.RUNTIME)
          public @interface GroupTODO {

           public enum Severity {CRITICAL, IMPORTANT, TRIVIAL, DOCUMENTATION};
           Severity severity() default Severity.IMPORTANT;
           String item();
           String assignedTo();
          }

          ---------------------
          運用annotation:

          package com.eric.news.cha06;

          @InProgress
          public class TestInProgress {

           @GroupTODO(
             severity=GroupTODO.Severity.CRITICAL,
             item="Figure out the amount of interest per month",
             assignedTo="Eric Zhou"
           )
           public void calculateInterest(float amount, float rate) {
            
           }
          }


          ---------------------

          測試:

          package com.eric.news.cha06;

          import Java.lang.reflect.Method;

          public class TestAnnotation {


           public static void main(String[] args) throws SecurityException, NoSuchMethodException {
            Class c = TestInProgress.class;
            Method element = c.getMethod("calculateInterest", float.class, float.class);
            
            GroupTODO groupTodo = element.getAnnotation(GroupTODO.class);
            String assignedTo = groupTodo.assignedTo();
            
            System.out.println(assignedTo);
            System.out.println(groupTodo.item());

           }

          }


          ---------------------
          結果:
          Eric Zhou
          Figure out the amount of interest per month


          posted on 2008-04-30 15:25 周銳 閱讀(199) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 巴塘县| 林芝县| 湟中县| 兴和县| 芒康县| 宣城市| 武冈市| 武川县| 吉安市| 盐津县| 玛多县| 丰原市| 新晃| 南木林县| 江源县| 巍山| 玉门市| 尼木县| 四川省| 台安县| 博客| 鹤山市| 肃北| 蓬溪县| 石景山区| 正安县| 高邑县| 云南省| 乌拉特中旗| 全州县| 盐池县| 常山县| 德州市| 乐昌市| 盐津县| 库车县| 普定县| 林周县| 正安县| 綦江县| 桃源县|