JAVA打怪之路 - 枚舉類與註解

枚舉類與註解

一、枚舉類的使用

說明:
1、 類的對象只有有限個,確定的,當需要定義一組常量時,強烈建議使用枚舉類。
2、 JDK1.5之前需要自定義枚舉類。
3、 JDK 1.5 新增的 enum 關鍵字 用於定義枚舉類。
4、 若枚舉只有一個對象, 則可以作爲一種單例模式的實現方式。

① 自定義枚舉類
在這裏插入圖片描述

class Season{
    private final String SEASONNAME;//季節的名稱
    private final String SEASONDESC;//季節的描述
    private Season(String seasonName,String seasonDesc){
        this.SEASONNAME = seasonName;
        this.SEASONDESC = seasonDesc;
    }
    public static final Season SPRING = new Season("春天", "春暖花開");
    public static final Season SUMMER = new Season("夏天", "夏日炎炎");
    public static final Season AUTUMN = new Season("秋天", "秋高氣爽");
    public static final Season WINTER = new Season("冬天", "白雪皚皚");
}

② 使用enum定義枚舉類
在這裏插入圖片描述

public enum SeasonEnum {
    SPRING("春天","春風又綠江南岸"),
    SUMMER("夏天","映日荷花別樣紅"),
    AUTUMN("秋天","秋水共長天一色"),
    WINTER("冬天","窗含西嶺千秋雪");
    private final String seasonName;
    private final String seasonDesc;
    private SeasonEnum(String seasonName, String seasonDesc) {
        this.seasonName = seasonName;
        this.seasonDesc = seasonDesc;
    }
    public String getSeasonName() {
        return seasonName;
    }
    public String getSeasonDesc() {
        return seasonDesc;
    }
}

③ Enum類的主要方法

values()方法:返回枚舉類型的對象數組。該方法可以很方便地遍歷所有的枚舉值。

valueOf(String str):可把一個字符串轉爲對應的枚舉類對象。要求字符串必須是枚舉類對象的“名字”。如不是,有運行時異常IllegalArgumentException。
toString():返回當前枚舉類對象常量的名稱。

在這裏插入圖片描述
二、註解的使用(Annotation)

① 註解的概述

1.1 jdk 5.0 新增的功能。

1.2 Annotation 其實就是代碼裏的特殊標記, 這些標記可以在編譯, 類加載, 運行時被讀取, 並執行相應的處理。通過使用 Annotation,程序員可以在不改變原有邏輯的情況下, 在源文件中嵌入一些補充信息。

1.3 在JavaSE中,註解的使用目的比較簡單,例如標記過時的功能,忽略警告等。在JavaEE/Android中註解佔據了更重要的角色,例如用來配置應用程序的任何切面,代替JavaEE舊版中所遺留的繁冗代碼和XML配置等。

② 常見的Annotation示例
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
③ 自定義 Annotation
在這裏插入圖片描述

MyAnnotation(value="尚硅谷")
public class MyAnnotationTest {
    public static void main(String[] args) {
         Class clazz = MyAnnotationTest.class;
         Annotation a = clazz.getAnnotation(MyAnnotation.class);
         MyAnnotation m = (MyAnnotation) a;
         String info = m.value();
         System.out.println(info);
      } 
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface MyAnnotation{
       String value() default "auguigu";
}

④ JDK 中的元註解

在這裏插入圖片描述

public enum RetentionPolicy{
  SOURCE,
  CLASS,
  RUNTIME
}
@Retention(RetentionPolicy.SOURCE)
@interface MyAnnotation1{  }
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation2{  }

在這裏插入圖片描述
在這裏插入圖片描述
⑤ JDK8中註解的新特性 : 可重複的註解 及 可用於類型的註解
在這裏插入圖片描述
在這裏插入圖片描述

@MyAnnotation
public class AnnotationTest<U> {
    @MyAnnotation
    private String name;
    public static void main(String[] args) {
         AnnotationTest<@MyAnnotation String> t = null;
         int a = (@MyAnnotation int) 2L;
         @MyAnnotation
         int b = 10;
    }
    public static <@MyAnnotation T> void method(T t) {
    }
    public static void test(@MyAnnotation String arg) throws @MyAnnotation Exception {
    }
}
@Target(ElementType.TYPE_USE)
@interface MyAnnotation {
    
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章