第十三章 枚举类和注解

た 入场券 2023-02-28 15:20 138阅读 0赞

目录

    • 13.1、枚举类
      • 13.1.1、概述
      • 13.1.2、自定义枚举类
        • 13.1.2.1、第一版
        • 13.1.2.2、第二版
        • 13.1.2.3、第三版
        • 13.1.2.4、测试方法
      • 13.1.3、系统的枚举类
        • 13.1.3.1、第一版
        • 13.1.3.2、第二版
        • 13.1.3.3、第三版
        • 13.1.3.4、测试方法
      • 13.1.4、常见方法
      • 13.1.5、注意事项
    • 13.2、注解
      • 13.2.1、概述
      • 13.2.2、元注解
        • 13.2.2.1、@interface注解
        • 13.2.2.2、@Inherited注解
        • 13.2.2.3、@Document注解
        • 13.2.2.4、@Target注解
        • 13.2.2.5、@Retention注解
      • 13.2.3、参数成员
      • 13.2.4、常见注解
      • 13.2.5、综合案例

13.1、枚举类

13.1.1、概述

概述:枚举是指将变量的值一一列出来,变量的值只限于列举出来的值的范围内。举例:一周只有7天,一年只有12个月等

格式:public enum 枚举名称 {}

13.1.2、自定义枚举类

13.1.2.1、第一版

  1. public class Direction1 {
  2. // 创建几个实例
  3. public static final Direction1 FRONT = new Direction1();
  4. public static final Direction1 BEHIND = new Direction1();
  5. public static final Direction1 LEFT = new Direction1();
  6. public static final Direction1 RIGHT = new Direction1();
  7. // 私有构造方法
  8. private Direction1() { }
  9. }

13.1.2.2、第二版

  1. public class Direction2 {
  2. // 创建几个实例
  3. public static final Direction2 FRONT = new Direction2("前");
  4. public static final Direction2 BEHIND = new Direction2("后");
  5. public static final Direction2 LEFT = new Direction2("左");
  6. public static final Direction2 RIGHT = new Direction2("右");
  7. // 私有构造方法
  8. private Direction2(String name) {
  9. this.name = name;
  10. }
  11. // 加入成员变量
  12. private String name;
  13. public String getName() {
  14. return name;
  15. }
  16. }

13.1.2.3、第三版

  1. public abstract class Direction3 {
  2. // 创建几个实例
  3. public static final Direction3 FRONT = new Direction3("前") {
  4. @Override
  5. public void show() {
  6. System.out.println("前");
  7. }
  8. };
  9. public static final Direction3 BEHIND = new Direction3("后") {
  10. @Override
  11. public void show() {
  12. System.out.println("后");
  13. }
  14. };
  15. public static final Direction3 LEFT = new Direction3("左") {
  16. @Override
  17. public void show() {
  18. System.out.println("左");
  19. }
  20. };
  21. public static final Direction3 RIGHT = new Direction3("右") {
  22. @Override
  23. public void show() {
  24. System.out.println("右");
  25. }
  26. };
  27. // 加入成员变量
  28. private String name;
  29. // 私有构造方法
  30. private Direction3(String name) {
  31. this.name = name;
  32. }
  33. public String getName() {
  34. return name;
  35. }
  36. // 加入抽象方法
  37. public abstract void show();
  38. }

13.1.2.4、测试方法

  1. public class DirectionDemo {
  2. public static void main(String[] args) {
  3. Direction1 d1 = Direction1.FRONT;
  4. System.out.println(d1);
  5. System.out.println("----------");
  6. Direction2 d2 = Direction2.FRONT;
  7. System.out.println(d2);
  8. System.out.println(d2.getName());
  9. System.out.println("----------");
  10. Direction3 d3 = Direction3.FRONT;
  11. System.out.println(d3);
  12. System.out.println(d3.getName());
  13. d3.show();
  14. }
  15. }

13.1.3、系统的枚举类

13.1.3.1、第一版

  1. public enum Direction1 {
  2. FRONT, BEHIND, LEFT, RIGHT;
  3. }

13.1.3.2、第二版

  1. public enum Direction2 {
  2. FRONT("前"), BEHIND("后"), LEFT("左"), RIGHT("右");
  3. // 添加成员变量
  4. private String name;
  5. // 私有构造方法
  6. private Direction2(String name) {
  7. this.name = name;
  8. }
  9. public String getName() {
  10. return name;
  11. }
  12. }

13.1.3.3、第三版

  1. public enum Direction3 {
  2. FRONT("前") {
  3. @Override
  4. public void show() {
  5. System.out.println("前");
  6. }
  7. },
  8. BEHIND("后") {
  9. @Override
  10. public void show() {
  11. System.out.println("后");
  12. }
  13. },
  14. LEFT("左") {
  15. @Override
  16. public void show() {
  17. System.out.println("左");
  18. }
  19. },
  20. RIGHT("右") {
  21. @Override
  22. public void show() {
  23. System.out.println("右");
  24. }
  25. };
  26. // 添加成员变量
  27. private String name;
  28. // 私有构造方法
  29. private Direction3(String name) {
  30. this.name = name;
  31. }
  32. public String getName() {
  33. return name;
  34. }
  35. // 添加抽象方法
  36. public abstract void show();
  37. }

13.1.3.4、测试方法

  1. public class DirectionDemo {
  2. public static void main(String[] args) {
  3. Direction1 d1 = Direction1.FRONT;
  4. System.out.println(d1);
  5. System.out.println("-------------");
  6. Direction2 d2 = Direction2.FRONT;
  7. System.out.println(d2);
  8. System.out.println(d2.getName());
  9. System.out.println("-------------");
  10. Direction3 d3 = Direction3.FRONT;
  11. System.out.println(d3);
  12. System.out.println(d3.getName());
  13. d3.show();
  14. System.out.println("--------------");
  15. Direction3 dd = Direction3.FRONT;
  16. switch (dd) {
  17. case FRONT:
  18. System.out.println("你选择了前");
  19. break;
  20. case BEHIND:
  21. System.out.println("你选择了后");
  22. break;
  23. case LEFT:
  24. System.out.println("你选择了左");
  25. break;
  26. case RIGHT:
  27. System.out.println("你选择了右");
  28. break;
  29. }
  30. }
  31. }

13.1.4、常见方法

  1. public class EnumMethodDemo {
  2. public static void main(String[] args) {
  3. // int compareTo(E o)
  4. Direction2 d21 = Direction2.FRONT;
  5. Direction2 d22 = Direction2.BEHIND;
  6. Direction2 d23 = Direction2.LEFT;
  7. Direction2 d24 = Direction2.RIGHT;
  8. System.out.println(d21.compareTo(d24));
  9. System.out.println(d22.compareTo(d23));
  10. System.out.println(d23.compareTo(d22));
  11. System.out.println(d24.compareTo(d21));
  12. System.out.println("---------------");
  13. // String name()
  14. System.out.println(d21.name());
  15. System.out.println(d22.name());
  16. System.out.println(d23.name());
  17. System.out.println(d24.name());
  18. System.out.println("--------------");
  19. // int ordinal()
  20. System.out.println(d21.ordinal());
  21. System.out.println(d22.ordinal());
  22. System.out.println(d23.ordinal());
  23. System.out.println(d24.ordinal());
  24. System.out.println("--------------");
  25. // String toString()
  26. System.out.println(d21.toString());
  27. System.out.println(d22.toString());
  28. System.out.println(d23.toString());
  29. System.out.println(d24.toString());
  30. System.out.println("--------------");
  31. // <T> T valueOf(Class<T> type,String name)
  32. Direction2 d = Enum.valueOf(Direction2.class, "FRONT");
  33. System.out.println(d.getName());
  34. System.out.println("----------------");
  35. // values()
  36. Direction2[] directions = Direction2.values();
  37. for (Direction2 direction : directions) {
  38. System.out.println(direction + ":" + direction.getName());
  39. }
  40. }
  41. }

13.1.5、注意事项

  1. 定义枚举类要用关键字enum
  2. 所有枚举类都是Enum的子类
  3. 枚举类的第一行上必须是枚举项,最后一个枚举项后的分号是可以省略的,但是如果枚举类有其它的东西,这个分号就不能省略,建议不要省略
  4. 枚举类可以有构造器,但必须是private的,它默认的也是private的。枚举项的用法比较特殊:枚举(“”);
  5. 枚举类也可以有抽象方法,但是枚举项必须重写该方法
  6. 枚举在switch语句中的使用

13.2、注解

13.2.1、概述

Java 注解(Annotation)又称 Java 标注,是 JDK5.0 引入的一种注释机制。Java 语言中的类、方法、变量、参数和包等都可以被注解。和 Javadoc 不同,Java 注解可以通过反射获取注解内容。在编译器生成类文件时,注解可以被嵌入到字节码中。Java 虚拟机可以保留注解内容,在运行时可以获取到注解内容 。 当然它也支持自定义 Java 注解。

格式:public @interface 注解名称 {}

13.2.2、元注解

13.2.2.1、@interface注解

使用 @interface 定义注解时,意味着它实现了 java.lang.annotation.Annotation 接口,即该注解就是一个Annotation,定义 Annotation 时,@interface 是必须的,通过 @interface 定义注解后,该注解不能继承其它的注解或接口

13.2.2.2、@Inherited注解

表示允许子类继承父类中的注解

13.2.2.3、@Document注解

表示将此注解包含在 javadoc 中

13.2.2.4、@Target注解

表示该注解目标

  • ElemenetType.CONSTRUCTOR 构造器声明
  • ElemenetType.FIELD 域声明
  • ElemenetType.LOCAL_VARIABLE 局部变量声明
  • ElemenetType.METHOD 方法声明
  • ElemenetType.PACKAGE 包声明
  • ElemenetType.PARAMETER 参数声明
  • ElemenetType.TYPE 类、接口、枚举、注解声明

13.2.2.5、@Retention注解

表示该注解的生命周期

  • RetentionPolicy.SOURCE 源码期间有效
  • RetentionPolicy.CLASS 编译期间有效
  • RetentionPolicy.RUNTIME 运行期间有效

13.2.3、参数成员

  1. 参数成员只能用public或默认(default)这两个访问权修饰
  2. 参数成员只能用八种基本数据类型(byte,short,int,long,float,double,char,boolean)和String、Enum、Class、Annotations等类型以及这些类型的一维数组

13.2.4、常见注解

  1. @Inherited 只能被用来标注“Annotation类型”,它所标注的Annotation具有继承性。
  2. @Documented 所标注内容,可以出现在javadoc中。
  3. @Target 只能被用来标注“Annotation类型”,而且它被用来指定AnnotationElementType属性。
  4. @Retention 只能被用来标注“Annotation类型”,而且它被用来指定AnnotationRetentionPolicy属性。
  5. @Deprecated 所标注内容,不再被建议使用。
  6. @Override 只能标注方法,表示该方法覆盖父类中的方法。
  7. @SuppressWarnings 所标注内容产生的警告,编译器会对这些警告保持静默。
  8. @FunctionalInterface 所标注接口代表是一个函数式接口。

13.2.5、综合案例

第一步:创建自定义注解,MyTest.java

  1. import java.lang.annotation.ElementType;
  2. import java.lang.annotation.Retention;
  3. import java.lang.annotation.RetentionPolicy;
  4. import java.lang.annotation.Target;
  5. @Retention(RetentionPolicy.RUNTIME)
  6. @Target(ElementType.METHOD)
  7. public @interface MyTest {
  8. public long timeout() default -1;
  9. }

第二步:创建待测试模块,UserDao.java

  1. public class UserDao {
  2. static {
  3. System.out.println("静态代码块执行了");
  4. }
  5. @MyTest
  6. public void addUser() {
  7. System.out.println("增加用户");
  8. }
  9. @MyTest
  10. public void delUser() {
  11. System.out.println("删除用户");
  12. }
  13. @MyTest
  14. public void uptUser() {
  15. System.out.println("更新用户");
  16. }
  17. @MyTest
  18. public void getUser() {
  19. System.out.println("获取用户");
  20. }
  21. }

第三步:反射执行方法,MyJunit.java

  1. import java.lang.reflect.Method;
  2. public class MyJunit {
  3. public static void main(String[] args) throws Exception {
  4. // 获取字节码文件
  5. Class<UserDao> clazz = UserDao.class;
  6. // 获取所有的方法
  7. Method[] mds = clazz.getMethods();
  8. // 遍历所有的方法
  9. for (Method md : mds) {
  10. if (md.isAnnotationPresent(MyTest.class)) {
  11. md.invoke(new UserDao());
  12. }
  13. }
  14. }
  15. }

发表评论

表情:
评论列表 (有 0 条评论,138人围观)

还没有评论,来说两句吧...

相关阅读

    相关 注解

    推荐使用枚举定义常量 1、枚举常量更简单 2、枚举常量属于稳态型 3、枚举具有内置方法 4、枚举可以自定义方法 枚举类型是不能有继承的,也就是说一个枚举常量定义