当前位置:网站首页>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-4) digital IC Verification: test point decomposition
- Who do you want to know when opening a stock account? Is it safe to open an account online now?
- 在按钮禁用时消除hover效果
- 组合数据类型之元组小练习
- Why install an SSL certificate on a web site?
- Which is the best billing method for okcc call center
- MySQL详解 --- 聚合与分组
- 大三下期末考試
- Scala 基礎 (三):運算符和流程控制
- Is there any risk in opening an account for Dongfang fortune stock? Is it safe for Dongfang fortune to open an account
猜你喜欢
Scala 基礎 (三):運算符和流程控制
![[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

SAP ui5 beginner tutorial 22 - development and use of filter
![[redis] key hierarchy](/img/ab/a5d3bb61b4571966d0f47037af4f41.png)
[redis] key hierarchy

What is the Valentine's Day gift given by the operator to the product?

chrome浏览器关闭更新弹窗

KOA Quick Start

Redis data migration (III)
![[redis] get to know redis for the first time](/img/02/3c6a7f6ea8c563386a4cd458024728.png)
[redis] get to know redis for the first time

如何成为一名高级数字 IC 设计工程师(4-2)脚本篇:Verilog HDL 代码实现的文件读写操作
随机推荐
利用kubernetes資源鎖完成自己的HA應用
Redis data migration (III)
基于 FPGA 的 RISC CPU 设计(4)关于项目的 36 个问题及其答案
【Redis】数据介绍 & 通用命令 & String类型
[MySQL practice of high concurrency, high performance and high availability of massive data -9] - transaction concurrency control solutions lbcc and mvcc
Cross border information station
如何成为一名高级数字 IC 设计工程师(6-7)数字 IC 验证篇:DEBUG 技巧
CTFHub-Web-密码口令-默认口令
[redis] list type
数字 IC 设计、FPGA 设计秋招笔试题目、答案、解析(1)2022 紫光展锐(上)
如何成为一名高级数字 IC 设计工程师(4-2)脚本篇:Verilog HDL 代码实现的文件读写操作
[從零開始學習FPGA編程-49]:視野篇 - 芯片是如何被設計出來的?
11 go Foundation: Interface
How to become a senior digital IC Design Engineer (4-2) script: file read / write operation realized by Verilog HDL code
数字 IC 设计、FPGA 设计秋招笔试题目、答案、解析(2)2021 华为海思(上)
Would like to ask how to choose a securities firm? Is it safe to open an account online now?
如何成为一名高级数字 IC 设计工程师(6-4)数字 IC 验证篇:测试点分解
How to become a senior digital IC Design Engineer (6-7) digital IC Verification: debug skills
How to become a senior digital IC Design Engineer (1-1) Verilog coding Grammar: Introduction
微信运动自动点赞