当前位置:网站首页>(六)枚举、注解
(六)枚举、注解
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修饰的注解,则其子类将自动具有该注解。
三、常用类
边栏推荐
- False positives and false negatives in testing are equally worthy of repeated corrections
- Use of QML
- Based on the local, linking the world | Schneider Electric "Industrial SI Alliance" joins hands with partners to go to the future industry
- 【CocosCreator 3.5】CocosCreator 获取网络状态
- WebSocket Session is null
- (树) 最近公共祖先(LCA)
- Port inspection steps - 7680 port analysis - Dosvc service
- MultipartFile文件上传
- IDEA常用快捷键与插件
- 浅识Flutter 基本组件之CheckboxListTile组件
猜你喜欢
STM32 problem collection
What is a system?
MultipartFile file upload
Detailed explanation of TCP (2)
Ambiguous method call.both
With 7 years of experience, how can functional test engineers improve their abilities step by step?
想从手工测试转岗自动化测试,需要学习哪些技能?
Safety 20220712
IDEA comment report red solution
Detailed explanation of TCP and UDP
随机推荐
Zotero如何删除自动生成的标签
Port inspection steps - 7680 port analysis - Dosvc service
CloudCompare & PCL calculate the degree of overlap between two point clouds
What is SQALE
【Cocos Creator 3.5】缓动系统停止所有动画
端口排查步骤-7680端口分析-Dosvc服务
How to develop a high-quality test case?
A brief introduction to the showDatePicker method of the basic components of Flutter
递归查询单表-单表树结构-(自用)
Ambiguous method call.both
VS QT - ui does not display newly added members (controls) || code is silent
BUG definition of SonarQube
QML的使用
安全20220718
Day32 LeetCode
Understanding and Using Unity2D Custom Scriptable Tiles (4) - Start to build a custom tile based on the Tile class (below)
What skills do I need to learn to move from manual testing to automated testing?
【AUTOSAR-RTE】-5-Explicit(显式)和Implicit(隐式) Sender-Receiver communication
SIP协议标准和实现机制
Safety 20220712