Java註解Annotation學習筆記

來源:互聯網
上載者:User

標籤:string   表示   value   equal   retention   type   document   src   round   

一、自訂註解1. 使用關鍵字 @interface2. 預設情況下,註解可以修飾 類、方法、介面等。3. 如下為一個基本的註解例子:
  1. //註解中的成員變數非常像定義介面
  2. public @interface MyAnnotation {
  3. //不帶有預設值
  4. String name();
  5. //帶有預設值
  6. int age() default 20;
  7. }
4. Reflect中3個常見方法解讀
  • getAnnotion(Class<T> annotationClass) 返回該元素上存在的,指定類型的註解,如果沒有返回null
  • Annotation[] getAnnotations() 返回該元素上所有的註解。
  • boolean isAnnotationPersent(Class<? extends Annotation> annotationClass) 判斷該院上上是否含有制定類型的注釋。
二、元註解
  1. @Target(ElementType.METHOD)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. public @interface ValueBind {
  4. filedType type();
  5. String value() default "SUSU";
  6. enum filedType {
  7. STRING, INT
  8. }
  9. }
其中@Target 和 @Retention為元註解。
  • @Retention有如下幾種取值:

  • 其中通常都是RetentionPolicy.RUNTIME

 @Target有如下幾個取值:
@Inherited 表示該註解具有繼承性。三、Demo
1. ValueBind註解
  1. @Target(ElementType.METHOD)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. public @interface ValueBind {
  5. filedType type();
  6. String value() default "SUSU";
  7. enum filedType {
  8. STRING, INT
  9. }
  10. }


2. Person類
  1. public class Person implements Serializable {
  2. private String name;
  3. private int age;
  4. public String getName() {
  5. return name;
  6. }
  7. @ValueBind(type = ValueBind.filedType.STRING, value = "LCF")
  8. public void setName(String name) {
  9. this.name = name;
  10. }
  11. public int getAge() {
  12. return age;
  13. }
  14. @ValueBind(type = ValueBind.filedType.INT, value = "21")
  15. public void setAge(int age) {
  16. this.age = age;
  17. }
  18. @Override
  19. public String toString() {
  20. return "Person{" +
  21. "name=‘" + name + ‘\‘‘ +
  22. ", age=" + age +
  23. ‘}‘;
  24. }
  25. }

3. Main方法
  1. import java.lang.reflect.Method;
  2. public class Main {
  3. public static void main(String[] args) throws Exception {
  4. Object person = Class.forName("Person").newInstance();
  5. //以下擷取的是:類的註解
  6. // Annotation[] annotations = person.getClass().getAnnotations();
  7. Method[] methods = person.getClass().getMethods();
  8. for (Method method : methods) {
  9. //如果該方法上面具有ValueBind這個註解
  10. if (method.isAnnotationPresent(ValueBind.class)) {
  11. //通過這個註解可以擷取到註解中定義的
  12. ValueBind annotation = method.getAnnotation(ValueBind.class);
  13. if (annotation.type().equals(ValueBind.filedType.STRING)) {
  14. method.invoke(person, annotation.value());
  15. } else {
  16. method.invoke(person, Integer.parseInt(annotation.value()));
  17. }
  18. }
  19. }
  20. System.out.println(person);
  21. }
  22. }
輸出結果:Person{name=‘LCF‘, age=21}






Java註解Annotation學習筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.