当前位置:网站首页>第10章 枚举类与注解
第10章 枚举类与注解
2022-07-27 10:46:00 【哈哈泡的茶】
目录
示例二:在编译时进行格式检查(JDK内置的三个基本注解)
3.如何自定义注解:参照@SuppressWarnings定义
一、枚举类的使用
1.枚举类的理解:类的对象只有有限个、确定的。我们称此类为枚举类。 如:季节、性别……
2.当需要定义一组常量时,强烈建议使用枚举类
3.如果枚举类中只有一个对象,则可以作为单例模式的实现方式。
二、如何自定义枚举类
方式一:jdk5.0之前,自定义枚举类
1.声明Season对象的属性:用private final修饰
2.私有化类的构造器,并给对象属性赋值
3.提供当前枚举类的多个对象:声明为public static final的
4.其他诉求1:获取枚举类对象的属性
5.其他诉求2:提供toString()
方式二:jdk5.0,可以使用enum关键字定义枚举类
1.提供当前枚举类的对象,多个对象之间用“,”隔开,末尾对象用“;”结束
2.声明Season对象的属性:用private final修饰
3.私有化类的构造器,并给对象属性赋值
4.其他诉求1:获取枚举类对象的属性
5.其他诉求2:提供toString()
valueOf(String objName): 返回枚举类中对象名是objName的对象。如果没有objName的枚举类对象,则抛异常:IllegalArgumentException 说明:定义的枚举类默认继承于java.lang.Enum类
三、Enum类中的常用方法
values()方法:返回枚举类型的对象数组。该方法可以很方便地遍历所有的枚举值。
valueOf(String str):可以把一个字符串转为对应的枚举类对象。要求字符串必须是枚举类对象。
toString()方法:返回当前枚举类对象常量的名称
四、使用enum关键字定义的枚举类实现接口的情况
情况一:实现接口,在enum类中实现抽象方法
情况二:让枚举类的对象分别实现接口中的抽象方法
五、注解(Annotation)
1.理解Annotation
①jdk5.0新增的功能
②Annotation其实就是代码里的特殊标记,这些标记可以在编译、类加载、运行时被读取,并执行相关的处理。通过使用Annotation,程序员可以在不改变原有逻辑的情况下,在源文件中嵌入一些补充信息。
③在javaSE中,注解的使用目的比较简单,例如标记过时的功能,忽略警告等。在javaSE/Android中注解占据了更重要的角色,例如用来配置应用程序的任何切面,代替javaEE旧版中所遗留的繁冗代码和XML配置等。
2.Annotation的使用示例
示例一:生成文档相关的注解

示例二:在编译时进行格式检查(JDK内置的三个基本注解)
@override: 限定重写父类方法,该注解只能用于方法
@deprecated: 用于表示所修饰的元素(类,方法等)已过时。通常是因为所修饰的结构危险或存在更好的选择。
@SuppressWarnings: 抑制编译器警告
示例三:跟踪代码依懒性,实现替代配置文件功能
3.如何自定义注解:参照@SuppressWarnings定义
①注解声明为:@interface
②内部定义成员,通常使用value表示
③可以指定成员的默认值,使用default定义
④如果自定义注解没有成员,表明是一个标识作用。
如果注解有成员,在使用注解时,需要指明成员的值。
自定义注解必须配上注解的信息处理流程(使用反射)才有意义。自定义注解通常都会指明两个元注解:Retention、Target

4.JDK提供的4种元注解
元注解:对现有的注解进行解释说明的注释
Retention: 指定所修饰的Annotation的生命周期:SOURCE\CLASS(默认行为)\RUNTIME,只有声明为RUNTIME生命周期的注解,才能通过反射获取。

Target:用于指定被修饰的Annotation能用于修饰哪些程序元素

******出现的频率较低******
Documented:表示所修饰的注解在被javadoc解析时,保留下来。
Inherited:被它修饰的Annotation将具有继承性。

5.通过反射获取注解信息 --- 到反射内容时系统讲解

6.JDK8中注解的新特性:可重复注解、类型注解
(1)可重复注解
①在MyAnnotation上声明@Repeatable,成员值为MyAnnotations.class
②MyAnnotation的Target和Retention等元注解与MyAnnotations相同。
(2)类型注解
ElementType.TYPE_PARAMETER 表示该注解能写在类型变量的声明语句中(如:泛型声明)
ElementType.TYPE_USE 表示该注解能写在使用类型的任何语句中。
边栏推荐
- An article reveals the NFT strategy of traditional game manufacturers such as Ubisoft
- Find the combination number acwing 888. find the combination number IV
- Markdown editor syntax - setting of text color, size, font and background color (Reprint)
- MySQL installation (RPM package)
- 求组合数 AcWing 886. 求组合数 II
- 6 find the smallest letter larger than the target letter
- Vscode establishes automatic search of header files under non engineering directories
- 求组合数 AcWing 887. 求组合数 III
- I've compromised. Since everyone wants to call me Yelin, there's nothing I can do
- 求组合数 AcWing 889. 满足条件的01序列
猜你喜欢

properties文件

最长上升子序列模型 AcWing 1012. 友好城市

Installation and use of GTEST and gmock

Find the combination number acwing 885. find the combination number I

Memory search acwing 901. Skiing

Inclusion exclusion principle acwing 890. divisible numbers

XXX packages are looking for funding run 'NPM fund' for details solutions

Gaussian elimination acwing 884. Gaussian elimination for solving XOR linear equations
![Take you hand-in-hand to develop a complete classic game [Tetris] from scratch, with less than 200 lines of logic.](/img/7f/f42f9267cdff35522c260ef073bab9.png)
Take you hand-in-hand to develop a complete classic game [Tetris] from scratch, with less than 200 lines of logic.

Solve importerror: cannot import name'abs'import tensorflow error
随机推荐
Find the combination number acwing 888. find the combination number IV
Stack acwing 3302. Expression evaluation
Knapsack problem acwing 9. grouping knapsack problem
[shader realizes shake random shaking effect _shader effect Chapter 10]
求组合数 AcWing 889. 满足条件的01序列
Find the combination number acwing 886. find the combination number II
8 find subsequences with a maximum length of K
数字三角形模型 AcWing 1027. 方格取数
Moveit2 - 1. Start
Yum source installation
9 UAV array
(8) Shell function
洛谷P1896 互不侵犯
Find the combination number acwing 885. find the combination number I
博弈论 AcWing 892. 台阶-Nim游戏
Kepserver configuration
KEPServer配置
Introduction to software vulnerability analysis (I)
Luogu p3052 [usaco12mar]cows in a skyscraper G
Markdown editor syntax - setting of text color, size, font and background color (Reprint)