1. 程式人生 > >java語言基礎--列舉,註解,正則和反射

java語言基礎--列舉,註解,正則和反射

註解

@Retention(RetentionPolicy.RUNTIME)//註解保留策略
public @interface MyAnno {
    String str();
    int val();
}
    @MyAnno(str = "測試註解",val = 100)
    public void test() {
        
    }

  所有註解都只包含方法宣告,不能提供方法體。應用註解時,需要為註解成員提供值

註解的保留策略,java指定三種保留策略,它們被封裝到java.lang.annotation.RetentionPolicy列舉中,

SOURCE ,只在原始檔保留,編譯器會被拋棄

CLASS 在編譯時儲存到.class檔案中,執行時不可通過jvm得到這些註解

RUNTIME  在編譯時儲存到.class檔案中,執行時可通過jvm得到這些註解

  因此,RUNTIME  是最永久的註解

使用反射在執行時獲取註解

@Retention(RetentionPolicy.RUNTIME)//註解保留策略
public @interface MyAnno {
    String str() default "OK";
    int val() default 500;
}
public
class Test { @MyAnno(str = "測試註解",val = 100) //這裡不指定值得時候,將會使用預設值 public static void myMeth() { Test t = new Test(); try { Class<?> clazz = t.getClass(); Method method = clazz.getMethod("myMeth"); MyAnno anno = method.getAnnotation(MyAnno.class
);//當註解策略不是RUNTIME時,這裡anno會得到null System.out.println(anno.str());//輸出 測試註解 System.out.println(anno.val());//輸出 100 } catch (NoSuchMethodException | SecurityException e) { e.printStackTrace(); } } public static void main(String[] args) { myMeth(); } }

 

先獲取當前物件,獲取Class物件 ,根據Class物件獲取當前Method,根據Method 獲取表明註解的值。

獲取所有註解

@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnno {
    String str();
    int val();
}
@Retention(RetentionPolicy.RUNTIME)
public @interface What {
    String desription();
}
@MyAnno(str="測試類註解",val=100)
@What(desription="類描述")
public class Meta2 {
    @MyAnno(str="測試方法註解",val=200)
    @What(desription="方法描述")
    public static void test() {
        Meta2 m= new Meta2();
        try {
            Annotation[] annos = m.getClass().getAnnotations();
            for (Annotation annotation : annos) {
                System.out.println(annotation);
            }
//            輸出如下 T8是包名
//            @T8.MyAnno(str=測試類註解, val=100)
//            @T8.What(desription=類描述)
            Method test = m.getClass().getMethod("test");
            Annotation[] annos2 = test.getAnnotations();
            for (Annotation annotation : annos2) {
                System.out.println(annotation);
            }
//            輸出如下 T8是包名
//            @T8.MyAnno(str=測試方法註解, val=200)
//            @T8.What(desription=方法描述)
        }catch (Exception e) {
            
        }
    }
    
    public static void main(String[] args) {
        test();
    }
}

單成員註解

@Retention(RetentionPolicy.RUNTIME)
public @interface MySingle {
    String value() default "aaa";//單成員註解僅僅包含一個成員 ,假設只有一個成員,使用註解時就可以直接為該成員設定,而不需要指定成員名稱,前提是成員名稱必須是value
}
public class Single {
    @MySingle("this is a single annotation")//這裡並沒有指定  value = 
    public static void test() throws Exception{
        Single s = new Single();
        Class<?> clazz = s.getClass();
        Method test = clazz.getMethod("test");
        MySingle mySingle = test.getAnnotation(MySingle.class);
        System.out.println(mySingle.value());//輸出  this is a single annotation
    }
    
    public static void main(String[] args)throws Exception {
        test();
    }
}

單值註解的其他用法

@Retention(RetentionPolicy.RUNTIME)
public @interface MySingle {
    String value() default "aaa";//這個仍然是單值成員
    
    int other() default 999;//其他成員必須有預設值
}
public class Single {
    @MySingle("this is a single annotation")//這裡並沒有指定  value = 
    public static void test() throws Exception{
        Single s = new Single();
        Class<?> clazz = s.getClass();
        Method test = clazz.getMethod("test");
        MySingle mySingle = test.getAnnotation(MySingle.class);
        System.out.println(mySingle.value());//輸出  this is a single annotation
        System.out.println(mySingle.other());//輸出  999
    }
    
    public static void main(String[] args)throws Exception {
        test();
    }
}

java的內建註解

@Retention 只能用於註解其他註解 ,用於指定保留策略

@Documented  只能用於註解其他註解 ,用於通知某個工具 註解將被文件化

@Target  只能用於註解其他註解 用於指定註解只能使用的場合,比如一下程式碼

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD,ElementType.CONSTRUCTOR})//這裡的作用是指定MySingle 只能作用在域變數或者建構函式
public @interface MySingle {
    
}

 

ElementType列舉值如下表

 

@Inherited   只能用於註解其他註解 允許子類繼承父類的註解。

@Override  方法重寫註解

@Deprecated 過期註解

@FunctionalInterface 函式式介面註解,用於指定一個介面是一個函式式介面(並不是必須條件,該介面僅僅是提供資訊)

@SuppressWarnings 抑制編譯器的警告註解