当前位置:网站首页>Day10 enumeration class and annotation
Day10 enumeration class and annotation
2022-06-29 02:14:00 【m0_ sixty-two million three hundred and sixty-one thousand six 】
1. Enumeration class
1. Custom enumeration class
1. Member variables cannot be changed private final
2. Private constructor private
3. example public static final
4.get Method
5.toString Method
package com.zkx.enumtest;
/**
* @author zkx
* @create 2022-02-14 13:05
*/
public class EnumTest {
public static void main(String[] args) {
Season spring = Season.SPRING;
System.out.println(spring);//Season{SEASONNAME='spring', SEASONDESC=' spring '}
System.out.println(spring.getSEASONNAME()+","+spring.getSEASONDESC());//spring, spring
}
}
// Create an enumeration class
class Season{
//1. First, the member variables cannot be changed private final
private final String SEASONNAME;// Season name
private final String SEASONDESC;// Seasonal description
//2. Constructor privatization private
private Season(String name , String desc){
this.SEASONNAME=name;
this.SEASONDESC=desc;
}
//3. The instance is declared as public static final
public static final Season SPRING = new Season("spring"," spring ");
public static final Season SUMMER = new Season("summer"," summer ");
public static final Season AUTUMN = new Season("autumn"," autumn ");
public static final Season WINTER = new Season("winter"," winter ");
//4.get Method , Because it is a private and unchangeable member variable , So there was no set Method
public String getSEASONNAME() {
return SEASONNAME;
}
public String getSEASONDESC() {
return SEASONDESC;
}
//5. rewrite toString Method
@Override
public String toString() {
return "Season{" +
"SEASONNAME='" + SEASONNAME + '\'' +
", SEASONDESC='" + SEASONDESC + '\'' +
'}';
}
}
2. Use enum Define an enumeration class
1. The first line must be an example , And public static final Automatic addition , Comma split , End of semicolon
2. The member variable is private final
3. Constructor must private
4.get Method
5. rewrite toString Method
Method :
1.values() Returns all instances of the enumeration class
2.valueOf(String str) Convert the string to the corresponding enumeration class , A string is the name of an enumerated class object , Otherwise, the report will be wrong
3.toString() Returns the name of the current enumeration class object
package com.zkx.enumtest;
/**
* @author zkx
* @create 2022-02-14 17:26
*/
public class EnumTest1 {
public static void main(String[] args) {
Season1 spring = Season1.SPRING;
System.out.println(spring);//Season1{seasonName='spring', seasonDesc=' spring '}
System.out.println("*************************");
//values() Method : Returns all instances of an enumerated class
Season1 [] values = Season1.values();
for (int i = 0; i < values.length; i++) {
System.out.println(values[i]);
}
System.out.println("*************************");
//valueOf(String str): Convert the string to the corresponding enumeration class , A string is the name of an enumerated class object
Season1 summer = Season1.valueOf("SUMMER");
System.out.println(summer);
}
}
// Use enum Define an enumeration class
enum Season1{// Cannot inherit other classes
//1. The instance must be on the first line , comma , Division , A semicolon ; ending , Instances are automatically added public static final
SPRING("spring"," spring "),
SUMMER("summer"," summer "),
AUTUMN("autumn"," autumn "),
WINTER("winter"," winter ");
//2. Member variables private final
private final String seasonName;
private final String seasonDesc;
//3. Constructor must private
private Season1(String seasonName,String seasonDesc){
this.seasonName=seasonName;
this.seasonDesc=seasonDesc;
}
//4.get Method
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
//5. rewrite toString Method
@Override
public String toString() {
return "Season1{" +
"seasonName='" + seasonName + '\'' +
", seasonDesc='" + seasonDesc + '\'' +
'}';
}
}
3. Use enum Define enumeration classes to implement interfaces
1. Enumeration class can implement interface
2. If all objects implement the same abstract methods of interfaces , You can override it once in the enumeration class
3. If all objects implement different abstract methods of interfaces , Override the interface abstract method in each instance of the enumerated class object
package com.zkx.enumtest;
/**
* @author zkx
* @create 2022-02-14 22:24
*/
public class EnumTest2 {
public static void main(String[] args) {
Time time1 = Time.AFTERNOON;
time1.show();
}
}
// Enumeration class implements the interface
interface Info{
void show();
}
// Enumeration class
enum Time implements Info{
//1. Examples must be written on the first line
MORNING("morning"," morning "){
@Override
public void show() {
System.out.println("zzz");
}
},
AFTERNOON("afternoon"," Afternoon "){
@Override
public void show() {
System.out.println("xxx");
},
},
EVENING("evening"," evening "){
@Override
public void show() {
System.out.println("www");
}
};
//2. Member variables private final
private final String timeName;
private final String timeDesc;
//3. Constructors private
private Time(String timeName,String timeDesc){
this.timeName = timeName;
this.timeDesc = timeDesc;
}
//4.get Method
public String getTimeName() {
return timeName;
}
public String getTimeDesc() {
return timeDesc;
}
//5.toString Method
@Override
public String toString() {
return "Time{" +
"timeName='" + timeName + '\'' +
", timeDesc='" + timeDesc + '\'' +
'}';
}
}
2. annotation Annotation


3.@Documented: Used to specify the Annotation Embellished Annotation Class will be javadoc Tool extraction as file . By default ,javadoc It doesn't include comments .
Defined as Documented The annotation for must be set Retention The value is RUNTIME.
边栏推荐
- 组合数据类型之元组小练习
- How to become a senior digital IC Design Engineer (6-6) digital IC Verification: system level simulation
- Google Borg论文
- Zhongyi technology resumed the review status of the gem IPO, and xuxiaofei no longer acted as a practicing lawyer
- Interviewer: with the for loop, why do you need foreach??
- HBuilder左侧工具栏不见了
- 我把整个研发中台拆分过程的一些心得总结
- How does flush open an account? Is it safe to open an account online now?
- 基于 FPGA 的 RISC CPU 设计(4)关于项目的 36 个问题及其答案
- Boost the digital economy and face the future office | the launch of the new version of spreadjsv15.0 is about to begin
猜你喜欢

OpenResty 使用介绍

Large scale visual relationship understanding
![[redis] sortedset type](/img/7f/f5f1aa603c8994b669d52a435fed7e.png)
[redis] sortedset type

OculusRiftS与Unity.UI的交互(1)-总览

Redis data migration (III)

【Redis】Set类型
![[MySQL practice of high concurrency, high performance and high availability of massive data -9] - transaction concurrency control solutions lbcc and mvcc](/img/62/77c2274db4f92ad1d88901e149251c.jpg)
[MySQL practice of high concurrency, high performance and high availability of massive data -9] - transaction concurrency control solutions lbcc and mvcc

【Redis】数据介绍 & 通用命令 & String类型

如何成为一名高级数字 IC 设计工程师(4-5)脚本篇:Shell 脚本实现的文件比较操作

基于 RISC-V SoC 的可配置 FFT 系统设计(1)引言
随机推荐
A full screen gesture adaptation scheme
【Redis】Key的层级结构
基于 RISC-V SoC 的可配置 FFT 系统设计(1)引言
网上联系客户经理办理炒股开户安全吗?
芯片原厂必学技术(1)引言
利用kubernetes資源鎖完成自己的HA應用
Secondary encapsulation of storage (sessionstorage/localstorage) using TS
QT basics tutorial: data types and containers
CTFHub-Web-密码口令-默认口令
利用kubernetes资源锁完成自己的HA应用
How to become a senior digital IC Design Engineer (4-2) script: file read / write operation realized by Verilog HDL code
Digital IC design, FPGA design written examination questions, answers and analysis of autumn move (1) 2022 Ziguang zhanrui (Part 1)
SAP ui5 beginner tutorial 22 - development and use of filter
How to become a senior digital IC Design Engineer (6-6) digital IC Verification: system level simulation
如何成为一名高级数字 IC 设计工程师(6-6)数字 IC 验证篇:系统级仿真
微信运动自动点赞
Configurable FFT system design based on risc-v SOC (1) Introduction
[從零開始學習FPGA編程-49]:視野篇 - 芯片是如何被設計出來的?
110. simple chat room 13: chat room server
11-Go基础:接口