枚举和注解

1 枚举

● 类的对象只有有限个,确定的。 使用场景:

  • 星期: Monday(星期一)、 …、 Sunday(星期天)
  • 性别: Man(男)、 Woman(女)
  • 季节: Spring(春节)…Winter(冬天)
  • 支付方式: Cash(现金)、 WeChatPay(微信)、 Alipay(支付宝)、 BankCard(银行卡)、 CreditCard(信用卡)
  • 就职状态: Busy、 Free、 Vocation、 Dimission
  • 订单状态: Nonpayment(未付款)、 Paid(已付款) 、 Delivered(已发货)、Return(退货)、Checked(已确认) Fulfilled(已配货)、
  • 线程状态:创建、就绪、运行、阻塞、死亡

● 当需要定义一组常量时,强烈建议使用枚举类 使用说明:

  • 使用 enum 定义的枚举类默认继承了 java.lang.Enum类,因此不能再继承其他类
  • 枚举类的构造器只能使用 private 权限修饰符
  • 枚举类的所有实例必须在枚举类中显式列出(, 分隔 ; 结尾)。列出的实例系统会自动添加 public static final 修饰
  • 必须在枚举类的第一行声明枚举类对象

● Enum类的主要方法:

  • values()方法:返回枚举类型的对象数组。该方法可以很方便地遍历所有的枚举值。
  • valueOf(String
    str):可以把一个字符串转为对应的枚举类对象。要求字符串必须是枚举类对象的“名字”。如不是,会有运行时异常:IllegalArgumentException。
  • toString():返回当前枚举类对象常量的名称

使用常量表示线程状态

public class StateDem1 {

    public static void main(String[] args) {
        System.out.println(State.WAITING);
    }


    static class State {
        public static final String NEW = "NEW";
        public static final String RUNNABLE = "RUNNABLE";
        public static final String BLOCKED = "BLOCKED";
        public static final String WAITING = "WAITING";
        public static final String TIMED_WAITING = "TIMED_WAITING";
        public static final String TERMINATED = "TERMINATED";
//        public static final String TERMINATED2 = "TERMINATED2";

    }

}

使用枚举表示线程状态

public class StateDem2 {

    int i;
    static int i1;


    public static void main(String[] args) {

        //获取一个枚举对象
        System.out.println(State.WAITING);
        System.out.println(State.valueOf("BLOCKED"));

        //循环枚举
        State[] states = State.values();
        for (int i = 0; i < states.length; i++) {
            System.out.println(states[i]);
        }
    }


    enum State {
        //一个类State,中有6个这个类的静态对象
        NEW,
        RUNNABLE,
        BLOCKED,
        WAITING,
        TIMED_WAITING,
        TERMINATED;
    }

}

订单状态的枚举

package a_enum;

/**
 *   订单状态: Nonpayment(未付款)、 Paid(已付款) 、 Delivered(已发货)、Return(退货)、 Checked(已确认) Fulfilled(已配货)、
 */
public class StateDemo3 {

    public static void main(String[] args) {
        OrderState[] states = OrderState.values();
        for (int i = 0; i < states.length; i++) {
            System.out.println(states[i] + "-" + states[i].getValue() + "-" + states[i].getCode());
        }
    }


    public enum OrderState {
        //static OrderState NON_PAYMENT = new OrderState("未付款")
        NON_PAYMENT(1, "未付款"),
        PAID(2, "已付款"),
        DELIVERED(3, "已发货"),
        RETURN(4, "退货"),
        CHECKED(5, "已确认"),
        FULFILLED(6, "已配货");


        private OrderState(int code, String value) {
            this.code = code;
            this.value = value;
        }

        private int code;
        private String value;

        public String getValue() {
            return value;
        }

        public int getCode() {
            return code;
        }
    }

}

2 注解

  • 从 JDK 5.0 开始, Java 增加了对元数据(MetaData) 的支持, 也就是Annotation(注解)
  • Annotation 其实就是代码里的特殊标记, 这些标记可以在编译, 类加载, 运行时被读取, 并执行相应的处理。通过使用Annotation, 程序员可以在不改变原有逻辑的情况下, 在源文件中嵌入一些补充信息。代码分析工具、开发工具和部署工具可以通过这些补充信息进行验证或者进行部署。
  • Annotation 可以像修饰符一样被使用, 可用于修饰包,类, 构造器, 方法, 成员变量, 参数, 局部变量的声明,这些信息被保存在Annotation的 “name=value” 对中。
  • 在JavaSE中,注解的使用目的比较简单,例如标记过时的功能,忽略警告等。在JavaEE/Android中注解占据了更重要的角色,例如用来配置应用程序的任何切面, 代替JavaEE旧版中所遗留的繁冗代码和XML配置等。
  • 未来的开发模式都是基于注解的, JPA是基于注解的, Spring2.5以上都是基于注解的,
    Hibernate3.x以后也是基于注解的,现在的Struts2有一部分也是基于注解的了,注解是一种趋势,一定程度上可以说: 框架 = 注解 + 反射 + 设计模式
  • 使用 Annotation 时要在其前面增加 @ 符号, 并把该 Annotation 当成一个修饰符使用。 用于修饰它支持的程序元素

2.1 常见的JDK注解

  • @Override: 限定重写父类方法, 该注解只能用于方法
  • @Deprecated: 用于表示所修饰的元素(类, 方法等)已过时。通常是因为所修饰的结构危险或存在更好的选择
  • @SuppressWarnings: 抑制编译器警告
public class AnnotationTest{
     public static void main(String[] args) {
         @SuppressWarnings("unused")
         int a = 10;
     }
     @Deprecated
     public void print(){
         System.out.println("过时的方法");
     }
     @Override
     public String toString() {
         return "重写的toString方法()";
     }
 }

2.2 JDK元注解

  • JDK 的元 Annotation 用于修饰其他 Annotation 定义

  • JDK5.0提供了4个标准的meta-annotation类型, 分别是:

    • Retention

    • Target

    • Documented

  • Inherited

  • @Retention: 只能用于修饰一个 Annotation 定义, 用于指定该 Annotation 的生命周期,
    @Rentention 包含一个 RetentionPolicy 类型的成员变量, 使用@Rentention 时必须为该 value
    成员变量指定值:

  • RetentionPolicy.SOURCE:在源文件中有效(即源文件保留) , 编译器直接丢弃这种策略的注释

  • RetentionPolicy.CLASS:在class文件中有效(即class保留) , 当运行 Java 程序时,
    JVM不会保留注解。 这是默认值

  • RetentionPolicy.RUNTIME:在运行时有效(即运行时保留) , 当运行 Java 程序时, JVM
    会保留注释。程序可以通过反射获取该注释。

  • @Target: 用于修饰 Annotation 定义, 用于指定被修饰的 Annotation 能用于修饰哪些程序元素。 @Target也包含一个名为 value 的成员变量。

  • JDK1.8@Target新增类型如下:

  • ElementType.TYPE_PARAMETER 表示该注解能写在类型变量的声明语句中(如: 泛型声明) 。

  • ElementType.TYPE_USE 表示该注解能写在使用类型的任何语句中。

  • Java 8新增可重复注解:

  • @Documented: 用于指定被该元 Annotation 修饰的 Annotation 类将被javadoc 工具提取成文档。默认情况下, javadoc是不包括注解的。

  • 定义为Documented的注解必须设置Retention值为RUNTIME。

  • @Inherited: 被它修饰的 Annotation 将具有继承性。如果某个类使用了被@Inherited 修饰的
    Annotation, 则其子类将自动具有该注解。

  • 比如:如果把标有@Inherited注解的自定义的注解标注在类级别上,子类则可以继承父类类级别的注解

2.3 通过反射获取注解

  • JDK 5.0 在 java.lang.reflect 包下新增了 AnnotatedElement 接口,
    该接口代表程序中可以接受注解的程序元素

  • 当一个 Annotation 类型被定义为运行时 Annotation 后, 该注解才是运行时可见, 当 class 文件被载入时保存在class 文件中的 Annotation 才会被虚拟机读取

  • 程序可以调用 AnnotatedElement对象的如下方法来访问 Annotation 信息
    在这里插入图片描述
    1. 常用JDK注解:

public class Parent {

    public void f1() {
        System.out.println("parent f1");
    }

    @Deprecated
    public void f2() {
        System.out.println("parent f2");
    }

    @SuppressWarnings("unused")
    public static void main(String[] args) {
        int i = 123;

        Parent p = new Children();
        p.f1();
        p.f2();
    }

}

class Children extends Parent {

    @Override
    public void f1() {
        System.out.println("children f1");
    }

    @Override
    public String toString() {
        return "Children{}";
    }

}

2. 简答注解

@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public @interface MyAnnotation {

}

使用注解:

public class Person {

    @MyAnnotation
    private String name;

    @MyAnnotation
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @MyAnnotation
    public void show() {
        System.out.println("show");
    }

    @MyAnnotation
    public void display() {
        System.out.println("display");
    }

}

测试:

public class Test1 {

    public static void main(String[] args) {
        try {
            Class clazz = Class.forName("c_annotation.Person");
            Object bean = clazz.newInstance();
//            Method show = clazz.getMethod("show");
//            show.invoke(bean); //反射调用方法

            //方法有注解@MyAnnotation执行,没有不执行
            Method[] methods = clazz.getMethods(); //得到所有的方法
            for (int i = 0; i < methods.length; i++) {
                if (methods[i].isAnnotationPresent(MyAnnotation.class)) { //判断注解是否存在
                    methods[i].invoke(bean);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }


    }

}

3. 复杂的注解

@Inherited
@Repeatable(MyAnnotations.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public @interface MyAnnotation {

    String value() default "hello";

}

@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public @interface MyAnnotation2 {

}

@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public @interface MyAnnotations {

    MyAnnotation[] value();

}


Person.java

public class Person {

    @MyAnnotation
    private String name;

    @MyAnnotation
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @MyAnnotation(value = "hello show")
    public void show() {
        System.out.println("show");
    }

    @MyAnnotation
    public void display() {
        System.out.println("display");
    }

    @MyAnnotations(value = {@MyAnnotation("hello1"), @MyAnnotation})
    public void f1() {

    }

//    @MyAnnotations(value = {@MyAnnotation("hello1"), @MyAnnotation("hello2")}) //等价于下面代码
    @MyAnnotation("hello1")
    @MyAnnotation("hello2")
    @MyAnnotation2
    public void f2() {

    }


}

使用反射得到注解

public class Person {

    @MyAnnotation
    private String name;

    @MyAnnotation
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @MyAnnotation(value = "hello show")
    public void show() {
        System.out.println("show");
    }

    @MyAnnotation
    public void display() {
        System.out.println("display");
    }

    @MyAnnotations(value = {@MyAnnotation("hello1"), @MyAnnotation})
    public void f1() {

    }

//    @MyAnnotations(value = {@MyAnnotation("hello1"), @MyAnnotation("hello2")}) //等价于下面代码
    @MyAnnotation("hello1")
    @MyAnnotation("hello2")
    @MyAnnotation2
    public void f2() {

    }


}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值