[Java 基础] 枚举、注解(九)

一、枚举

基本介绍

枚举是一组常量的集合
枚举属于一种特殊的类,里面只包含一组有限的特定的对象

二、枚举实现方式

1. 自定义类实现

  • 不需要提供setXxx方法,因为枚举对象值通常为只读
  • 对枚举对象/属性使用final + static共同修饰,实现底层优化
  • 枚举对象名通常使用全部大写,常量的命名规范
  • 枚举对象根据需要也可以有很多属性

1⃣️将构造器私有化
2⃣️在类直接创建固定对象
3⃣️对外暴露对象(通过为对象添加public final static 修饰符)
4⃣️可以提供get方法,不能提供set方法

2. enum关键字实现

  • 当我们使用enum关键字开发一个枚举类时,默认会继承Enum类,而且是一个final类
  • 如果使用无参构造器创建枚举对象,则实参列表和小括号都可以省略
  • 当有多个枚举对象时,使用’ , '号间隔,最后有一个分号结尾
  • 枚举对象必须放在枚举类的行首
  • 用enum关键字后不能再继承其它类了(隐式继承了Enum类)
  • 枚举类和普通类一样,可以实现接口

1⃣️使用enum替代class
2⃣️SUMMWE(“夏天” , “炎热”);
3⃣️如果有多个常量,用’ , '号隔开
4⃣️定义常量对象必须写在前面

三、enum常用方法

基本介绍

  1. toString:Enum类已经重写,返回对象名;子类可以重写,返回属性信息
  2. name() :返回对象名,子类不能重写
  3. ordinal():返回位置号,默认从0开始
  4. values():返回枚举类中的常量
  5. valueOf():将字符串转换成枚举对象
  6. compareTo():比较两个枚举常量的位置编号

四、注解

基本介绍

  • 注解也被称为元数据,用于修饰解释属性方法构造器局部变量
  • 和注释一样,注解不影响程序逻辑,但注解可以被编译/运行(相当于嵌入代码的补充信息)
  • 在JavaSE中,占普通作用,标记过时的功能,忽略警告
  • 在JavaEE中,占重要作用,配置应用程序的任何切面,代替JavaEE旧版中所繁冗代码和XML配置等

五、Annotation

基本介绍

使用Annotation时要在其前面增加@符号,并把自己当成一个修饰符使用,修饰它支持的程序元素

  1. @Overrid:限定某个方法,是重写父类对象,该注解只能用于方法
  2. @Depreca:用于表示某个程序元素(类、方法等)已过时
  3. @Suppress Warnings:抑制编译器警告

六、@Overrid注解

1. 基本介绍

限定某个方法,是重写父类对象,该注解只能用于方法

2. 基本语法

class Father{
	public void fly(){
		System.out.println("Father fly...");
	}
}
class Son extends Father{
	@Override
	public void fly(){
		System.out.println("Son fly...");
	}
}

3. 注意示例

  • 重写父类方法,如果父类没有方法,则会报错
  • 如果不写@Overrid注解,仍然可以构成重写
  • @Overrid只能修饰方法,不能修饰其它类、包、属性等等
  • @Overrid源码
@Target(ElementType.METHOD)	//仅限修饰方法
@Retention(RetentionPolicy.SOURCE)
public @interface Override {	//@interface表示注解类
}
  • @Target是修饰注解的注解,称为元注解
  • 写了@Overrid,编译器会检查该方法是否重写了父类方法,重写了编译通过,否则报错

七、@Deprecated注解

1. 基本介绍

用于表示某个程序元素(类、方法等)已过时

2. 基本语法

在这里插入图片描述

3. 注意示例

  • 可以修饰方法字段参数等等
  • @Deprecated的作用可以做到新旧版本的兼容和过度
  • @Deprecated源码
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, MODULE, PARAMETER, TYPE})
public @interface Deprecated {
    /**
     * Returns the version in which the annotated element became deprecated.
     * The version string is in the same format and namespace as the value of
     * the {@code @since} javadoc tag. The default value is the empty
     * string.
     *
     * @return the version string
     * @since 9
     */
    String since() default "";

    /**
     * Indicates whether the annotated element is subject to removal in a
     * future version. The default value is {@code false}.
     *
     * @return whether the element is subject to removal
     * @since 9
     */
    boolean forRemoval() default false;
}

八、@SuppressWarnings注解

1. 基本介绍

抑制编译器警告

2. 基本语法

@SuppressWarnings({"rewtypes","unchecked","unused"})

3. 注意示例

  • @SuppressWarnings({“rewtypes”,“unchecked”,“unused”})
  • unchecked:忽略没有检查的警告
  • rewtypes:忽略没有指定泛型的警告
  • unused:忽略没有使用某个变量的警告
  • 作用范围与放置位置有关
  • @SuppressWarnings源码
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, MODULE})
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
    /**
     * The set of warnings that are to be suppressed by the compiler in the
     * annotated element.  Duplicate names are permitted.  The second and
     * successive occurrences of a name are ignored.  The presence of
     * unrecognized warning names is <i>not</i> an error: Compilers must
     * ignore any warning names they do not recognize.  They are, however,
     * free to emit a warning if an annotation contains an unrecognized
     * warning name.
     *
     * <p> The string {@code "unchecked"} is used to suppress
     * unchecked warnings. Compiler vendors should document the
     * additional warning names they support in conjunction with this
     * annotation type. They are encouraged to cooperate to ensure
     * that the same names work across multiple compilers.
     * @return the set of warnings to be suppressed
     */
    String[] value();	//‼️
}

九、元注解

1. 基本介绍

元Annotation用于修饰其它注解Annotation

2. 种类

  1. @Retention:指定注解作用范围
  2. @Target:指定注解可以在哪使用
  3. @Documented:指定注解是否会在javadoc体现
  4. @Inherited:子类会继承父类的注解

十、@Retention注解

基本介绍

只能用于修饰一个Annotation定义,用于指定该Annotation可以保留多长时间
@Retention包含一个RetentionPolicy类型的成员变量

  1. RetentionPolicy.SOURCE:作用在源文件,使用后丢弃
  2. RetentionPolicy.CLASS:作用在class文件,JVM不会保留
  3. RetentionPolicy.RUNTIME:作用在JVM记录在class文件,运行时JVM保留,程序可以通过反射获取该注解
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你怎么知道我头发乌黑浓密

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值