1 枚举类

1.1自定义枚举类

  1. 构造器需要私有化
  2. 不需要提供set方法,因为枚举对象的值为只读
  3. 对枚举对象/属性使用 final + static 共同修饰,实现底层优化
  4. 枚举对象名通常使用大写
  5. 枚举对象根据需求,可以有多个属性
  1. package com.enum_;
  2. public class Enumeration01 {
  3. public static void main(String[] args) {
  4. System.out.println(Season.SPRING);
  5. System.out.println(Season.SUMMER);
  6. System.out.println(Season.AUTUMN);
  7. System.out.println(Season.WINTER);
  8. }
  9. }
  10. class Season{
  11. private String name;
  12. private String describe;
  13. //
  14. public final static Season SPRING = new Season("春天","温暖");
  15. public final static Season SUMMER = new Season("夏天","炎热");
  16. public final static Season AUTUMN= new Season("秋天","凉爽");
  17. public final static Season WINTER = new Season("冬天","寒冷");
  18. private Season(String name, String describe) {
  19. this.name = name;
  20. this.describe = describe;
  21. }
  22. public String getName() {
  23. return name;
  24. }
  25. public String getDescribe() {
  26. return describe;
  27. }
  28. @Override
  29. public String toString() {
  30. return "Season{" +
  31. "name='" + name + '\'' +
  32. ", describe='" + describe + '\'' +
  33. '}';
  34. }
  35. }

1.2 enum关键字

  • 相当于将对象的创建简化为对象名(参数),之间必须用逗号隔开,且定义要放在开头。
  • 若没有参数,可以简化为对象名直接定义。如:
  1. enum Gender{
  2. BOY,GIRL;
  3. }
  1. package com.enum_;
  2. public class Enumeration02 {
  3. public static void main(String[] args) {
  4. System.out.println(Season2.SPRING);
  5. System.out.println(Season2.SUMMER);
  6. System.out.println(Season2.AUTUMN);
  7. System.out.println(Season2.WINTER);
  8. }
  9. }
  10. enum Season2{
  11. SPRING("春天","温暖"),
  12. SUMMER("夏天","炎热"),
  13. AUTUMN("秋天","凉爽"),
  14. WINTER("冬天","寒冷");
  15. private String name;
  16. private String describe;
  17. private Season2(String name, String describe) {
  18. this.name = name;
  19. this.describe = describe;
  20. }
  21. public String getName() {
  22. return name;
  23. }
  24. public String getDescribe() {
  25. return describe;
  26. }
  27. @Override
  28. public String toString() {
  29. return "Season2{" +
  30. "name='" + name + '\'' +
  31. ", describe='" + describe + '\'' +
  32. '}';
  33. }
  34. }

1.3 Enum类中的各种方法

  • name:返回对象名
  • ordinal: 返回对象的位置号(从0开始)
  • values: 返回当前类中的所有常量(是一个数组)
  • valueOf(将字符串转化为枚举对象,但要求字符串必须是已有常量名
  1. Season value = Season.valueOf("SPRING");
  • toString:返回对象名
  1. package com.enum_;
  2. public class EnumExercise {
  3. public static void main(String[] args) {
  4. Gender boy = Gender.BOY;
  5. Gender boy2 = Gender.BOY;
  6. System.out.println(boy);
  7. System.out.println(boy == boy2);
  8. System.out.println("==============enum类的方法测试==========");
  9. System.out.println("name()函数:" + boy.name());
  10. System.out.println("ordinal()函数:" +boy.ordinal());
  11. Gender[] genders = Gender.values();
  12. for(Gender gender:genders){
  13. System.out.println("values()函数:" +gender);
  14. }
  15. Gender girl = Gender.valueOf("GIRL");
  16. System.out.println("valueOf()函数:" +girl);
  17. }
  18. }
  19. enum Gender{
  20. BOY,GIRL;
  21. }

2 Annotation(注解)

  • 基本的 Annotation
    1. @Override: 现定于某个方法,是重写父类的方法,该注解只能用于方法
    2. @Deprecated: 用于表示某个程序元素已过时
    3. @SuppressWarnings:抑制编译器警告

2.1 @Override

  • 使用@Override,表明该方法重写了父类的方法
  • 编译器会检查是否真的重写,若没有重写,编译器会报错
    -@Override的定义
  1. @Target(ElementType.METHOD)//说明只能修饰方法,Target是修饰注解的注解,称为元注解
  2. @Retention(RetentionPolicy.SOURCE)
  3. public @interface Override {//@interface表明是一个注解类
  4. }

2.2 @Deprecated

  • 用于表示某个程序元素已过时,不代表不能使用
  • 可以修饰方法,类,字段,包,参数

2.3 @SuppressWarnings

  • 不希望看到编译器的相关警告可以使用这注解加上相关的参数

2.4元注解

  • 修饰注解的注解

2.4.1 @Retention

  • Retention的三种值
    1. RententionPolicy.SOURCE:编译器使用后,直接丢弃这种策略的注解
    2. RententionPolicy.CLASS:编译器把注解记录在class文件中,当运行时,JVM不会保留
    3. RententionPolicy.RUNTIME:编译器把注解记录在class文件中,当运行时,JVM会保留,程序可以通过反射获取该注解

2.4.2 @Target

  • 用于指定被修饰的注解能修饰哪些程序元素

2.4.3 @Documented

  • 用于指定被该注解修饰的注解将被javadoc工具提取成文档,即生成文档时,可以看到该注解

2.4.4 @Inherited

  • 被改注解修饰的注解类具有继承性,即某个类被@Inherited修饰的注解修饰,其子类自动获得该注解。