当前位置:网站首页>(六)枚举、注解
(六)枚举、注解
2022-07-31 03:34:00 【来世做春风嘛】
一、枚举
1. 介绍
(1)枚举:(enumeration,简写enum)
(2)枚举是一组常量的集合
(3)枚举属于一种特殊的类,里面只包含一组有限的特定的对象
2. 枚举的二种实现方式
(1)自定义类实现枚举
(2)使用 enum 关键字实现枚举
3. 自定义类实现枚举
(1)不需要提供setXxx方法,因为枚举对象值通常为只读
(2)对枚举对象/属性使用 final + static 共同修饰,实现底层优化
(3)枚举对象名通常使用全部大写,常量的命名规范
(4)枚举对象根据需要,也可以有多个属性public class Season { String name; String desc; // 1.私有化构造器 private Season(String name, String desc) { this.name = name; this.desc = desc; } // 3. 直接创建固定对象 public final static Season SPRING = new Season("春天","温暖"); public final static Season SUMMER = new Season("夏天","炎热"); }
4. 使用 enum 关键字实现枚举
(1)使用 enum 关键字开发一个枚举类时,默认会继承Enum类
(2)如果使用无参构造器创建枚举对象,则实参列表和小括号都可以省略
(3)枚举对象必须放在枚举类的行首
public enum Season { SPRING("春天","温暖"), SUMMER("夏天","炎热"), WHAT;// 使用无参构造器创建枚举对象,则实参列表和小括号都可以省略 String name; String desc; private Season() { } private Season(String name, String desc) { this.name = name; this.desc = desc; } }
5. enum常用方法说明
使用关键字 enum 时,会隐式继承 Enum类,这样我们就可以使用 Enum类 相关的方法。
(1)toString:Enum类已经重写过了,返回的是当前对象名。子类可以重写该方法,用于返回对象的属性信息。
(2)name:返回当前对象名(常量名),子类中不能重写
(3)ordinal:返回当前对象的位置号,默认从0开始(4)values:返回当前枚举类中所有的常量
(5)valueof:将字符串转换成枚举对象,要求字符串必须为已有的常量名,否则报异常!
(6)compareTo:比较两个枚举常量,比较的就是位置号!
public static void main(String[] args) { Season spring = Season.SPRING; System.out.println(spring.toString()); System.out.println(spring.name()); System.out.println(spring.ordinal()); Season[] values = Season.values(); Season summer = Season.valueOf("SUMMER"); // spring编号 - summer编号 int i = spring.compareTo(summer); }
二、注解
1. 注解的介绍
(1)注解(Annotation)也被称为元数据(Metadata),用于修饰解释包、类、方法、属性、构造器、局部变量等数据信息。
(2)和注释一样,注解不影响程序逻辑,但注解可以被编译或运行,相当于嵌入在代码中的补充信息。
(3)在 JavaSE 中,注解的使用目的比较简单,例如标记过时的功能,忽略警告等。在JavaEE 中注解占据了更重要的角色,例如用来配置应用程序的任何切面,代替javaEE旧版中所遗留的繁冗代码和 XML配置 等。
2. 基本的Annotation介绍
使用Annotation时要在其前面增加@符号,并把该Annotation当成一个修饰符使用。用于修饰它支持的程序元素
三个基本的Annotation:
(1)@override:限定某个方法,是重写父类方法,该注解只能用于方法
(2)@Deprecated:用于表示某个程序元素(类,方法等)已过时
(3)@SuppressWarnings:抑制编译器警告
补充说明:@interface的说明
@interface 不是interface(接口),是注解类,是 jdk1.5 之后加入的。
3. 元注解(注解的注解)
3.1 元注解的种类
(1)Retention(保留):指定注解的作用范围,三种SOURCE、CLASS、RUNTIME
(2)Target(目标):指定注解可以在哪些地方使用
(3)Documented:指定该注解是否会在javadoc 体现
(4)Inherited(继承):子类会继承父类注解
3.2 @Retention注解
只能用于修饰一个注解定义,用于指定该注解可以保留多长时间,@Rentention 包含一个 RetentionPolicy类型 的成员变量,使用 @Rentention 时必须为该 value成员变量 指定值。
@Retention的三种值
(1)RetentionPolicy.SOURCE:编译器使用后,直接丢弃这种策略的注解
(2)RetentionPolicy.CLASS:编译器将把注解记录在 class文件 中,当运行Java程序时,JVM不会保留注解。这是默认值
(3)RetentionPolicy.RUNTIME:编译器将把注解记录在class文件中。当运行Java程序时,JVM会保留注解。程序可以通过反射获取该注解。
3.3 @Target 注解
用于修饰注解定义,用于指定被修饰的注解能用于修饰哪些程序元素。@Target 也包含一个名为 value 的成员变量。
3.4 @Documented 注解
@Documented:用于指定被该元注解修饰的注解类将被 javadoc 工具提取成文档,即在生成文档时,可以看到该注解。
说明:定义为 Documented 的注解必须设置 Retention 值为 RUNTIME。
3.4 @Inherited注解 注解(使用较少)
被它修饰的注解将具有继承性。如果某个类使用了被@lnherited修饰的注解,则其子类将自动具有该注解。
三、常用类
边栏推荐
- 注解用法含义
- The distance value between two arrays of LeetCode simple questions
- Why SocialFi achievement Web3 decentralized social in the future
- [Swift]自定义点击APP图标弹出的快捷方式
- What is SQALE
- With 7 years of experience, how can functional test engineers improve their abilities step by step?
- 浅识Flutter 基本组件之showDatePicker方法
- The use of beforeDestroy and destroyed
- C primer plus study notes - 8, structure
- Pytest e-commerce project combat (on)
猜你喜欢
![[C language] Preprocessing operation](/img/69/0aef065ae4061edaf0d96b89846bf2.png)
[C language] Preprocessing operation

MultipartFile file upload

Based on the local, linking the world | Schneider Electric "Industrial SI Alliance" joins hands with partners to go to the future industry

QML的使用

What is a system?

Just debuted "Fight to Fame", safety and comfort are not lost

SIP协议标准和实现机制

Key Technologies of Interface Testing

数据库实现分布式锁

LeetCode simple problem to find the subsequence of length K with the largest sum
随机推荐
Several common errors when using MP
[Dynamic programming] Maximum sum of consecutive subarrays
安全20220722
Zotero如何删除自动生成的标签
【异常】The field file exceeds its maximum permitted size of 1048576 bytes.
递归查询单表-单表树结构-(自用)
C primer plus study notes - 8, structure
How Zotero removes auto-generated tags
Atomic operation CAS
Redis implements distributed locks
els 方块向左移动条件判断
自己的一些思考
How to develop a high-quality test case?
[Godot][GDScript] 2D cave map randomly generated
Getting Started with CefSharp - winform
els 方块向右移
Know the showTimePicker method of the basic components of Flutter
TCP和UDP详解
观察者模式
【论文阅读】Mastering the game of Go with deep neural networks and tree search