Java中枚举的写法和用法

时间:2022-02-26 03:41:40

Java中枚举的写法和用法

span::selection, .CodeMirror-line > span > span::selection { background: #d7d4f0; }.CodeMirror-line::-moz-selection, .CodeMirror-line > span::-moz-selection, .CodeMirror-line > span > span::-moz-selection { background: #d7d4f0; }.cm-searching {background: #ffa; background: rgba(255, 255, 0, .4);}.cm-force-border { padding-right: .1px; }@media print { .CodeMirror div.CodeMirror-cursors {visibility: hidden;}}.cm-tab-wrap-hack:after { content: ""; }span.CodeMirror-selectedtext { background: none; }.CodeMirror-activeline-background, .CodeMirror-selected {transition: visibility 0ms 100ms;}.CodeMirror-blur .CodeMirror-activeline-background, .CodeMirror-blur .CodeMirror-selected {visibility:hidden;}.CodeMirror-blur .CodeMirror-matchingbracket {color:inherit !important;outline:none !important;text-decoration:none !important;}.CodeMirror-sizer {min-height:auto !important;}
-->
li {list-style-type:decimal;}.wiz-editor-body ol.wiz-list-level2 > li {list-style-type:lower-latin;}.wiz-editor-body ol.wiz-list-level3 > li {list-style-type:lower-roman;}.wiz-editor-body blockquote {padding: 0 12px;}.wiz-editor-body blockquote > :first-child {margin-top:0;}.wiz-editor-body blockquote > :last-child {margin-bottom:0;}.wiz-editor-body img {border:0;max-width:100%;height:auto !important;margin:2px 0;}.wiz-editor-body table {border-collapse:collapse;border:1px solid #bbbbbb;}.wiz-editor-body td,.wiz-editor-body th {padding:4px 8px;border-collapse:collapse;border:1px solid #bbbbbb;min-height:28px;word-break:break-word;box-sizing: border-box;}.wiz-hide {display:none !important;}
-->

       在公司代码中,用了一大堆的枚举,看得我好懵逼。下面开始看看枚举怎么写和怎么用。

一、枚举的写法

        关于枚举的写法,网上好多这方面的知识。这里直接贴一个我自己写的枚举类的代码。
(1)代码片段一
package com.tomtop.application.libraries.constentEnum;

/**
* 包材领用状态枚举类
* @author zeng.xiao.yan
* @date Jun 5, 2017 1:04:40 PM
* @version V1.0
*/ public enum UseStatusEnum { PACKING_NOT_USE(1), // 未领用
PACKING_HAS_USED(2), // 已领用
PACKING_BACK_USE(3); // 已打回 private Integer useStatus; private UseStatusEnum(int useStatus) {
this.useStatus = useStatus;
} public Integer getUseStatus() {
return useStatus;
} }
(2)代码片段二
package com.tomtop.application.libraries.constentEnum;
/**
* b2b发票状态枚举类
* @author ZENG.XIAO.YAN
* @date Aug 11, 2017 9:32:04 AM
* @version V1.0
* @qq 1927
*/ public enum B2bInvoiceStatusEnum { /** 待开票 */
WAIT_MAKE_INVOICE(10,"待开票"),
/** 待关务审核发票 */
WAIT_CUSTOMS_AUDIT_INVOICE(20,"待关务审核发票"),
/** 待财务审核发票 */
WAIT_FINANCIAL_AUDIT_INVOICE(30,"待财务审核发票"),
/** 待财务认证发票 */
WAIT_FINANCIAL_AUTHENTICATION_INVOICE(40,"待财务认证发票"),
/** 待税局退税 */
WAIT_TAX_STATION_TAX_REIMBURSEMENT(50,"待税局退税"),
/** 不可退税 */
CAN_NOT_TAX_REIMBURSEMENT(60,"不可退税"),
/** 已退税 */
HAS_TAX_REIMBURSEMENT(70,"已退税"); private Integer status;
private String text; //构造方法
private B2bInvoiceStatusEnum(Integer status,String text ){
this.status = status;
this.text = text;
} public Integer getStatus() {
return status;
} public String getText() {
return text;
} }

二、枚举类在代码中的用法

(1)代码片段一
//  枚举使用案例01
productPackingRecord.setUseStatus(UseStatusEnum.PACKING_NOT_USE.getUseStatus()); //设为未领用状态
            注意:这里调用getUseStatus这个方法才能返回枚举常量的值。
(2)代码片段二
    // 枚举使用案例02
public Map<String,String> getOptions(){
Map<String,String> options = new LinkedHashMap<>();
B2bInvoiceStatusEnum[] statusEnums = B2bInvoiceStatusEnum.values(); // 获得枚举对象数组
// 遍历枚举对象
for (B2bInvoiceStatusEnum b2bInvoiceStatusEnum : statusEnums) {
// eg:put("10","待开票")
options.put(b2bInvoiceStatusEnum.getStatus().toString(), b2bInvoiceStatusEnum.getText());
}
return options;
} // 枚举使用案例03
/** (3)保存发票,发票状态设为'待关务部审核发票' */
invoiceInfo.setStatus(B2bInvoiceStatusEnum.WAIT_CUSTOMS_AUDIT_INVOICE.getStatus());
session.save(invoiceInfo);
// 枚举使用案例02
public Map<String,String> getOptions(){
Map<String,String> options = new LinkedHashMap<>();
B2bInvoiceStatusEnum[] statusEnums = B2bInvoiceStatusEnum.values(); // 获得枚举对象数组
// 遍历枚举对象
for (B2bInvoiceStatusEnum b2bInvoiceStatusEnum : statusEnums) {
// eg:put("10","待开票")
options.put(b2bInvoiceStatusEnum.getStatus().toString(), b2bInvoiceStatusEnum.getText());
}
return options;
}

// 枚举使用案例03
/** (3)保存发票,发票状态设为'待关务部审核发票' */
invoiceInfo.setStatus(B2bInvoiceStatusEnum.WAIT_CUSTOMS_AUDIT_INVOICE.getStatus());
session.save(invoiceInfo);

                              http://blog.csdn.net/qiyueqinglian/article/details/50738548

 未完待续。。。。。。。。