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 {
    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值