Syntax and usage of enumerations in Java

Source: Internet
Author: User
Tags gettext

In the company code, with a lot of enumeration, I am very confused to see. Let's start by looking at how enumerations are written and how to use them. First, the enumeration of the wording of the enumeration, online a lot of this knowledge. Here is a code for the enumeration class that I write myself directly.
(1) Code snippet One
  
 
  1. package com.tomtop.application.libraries.constentEnum;
  2. /**
  3. * 包材领用状态枚举类
  4. * @author zeng.xiao.yan
  5. * @date Jun 5, 2017 1:04:40 PM
  6. * @version V1.0
  7. */
  8. public enum UseStatusEnum {
  9. PACKING_NOT_USE(1), // 未领用
  10. PACKING_HAS_USED(2), // 已领用
  11. PACKING_BACK_USE(3); // 已打回
  12. private Integer useStatus;
  13. private UseStatusEnum(int useStatus) {
  14. this.useStatus = useStatus;
  15. }
  16. public Integer getUseStatus() {
  17. return useStatus;
  18. }
  19. }

Package <textarea style="display: none">com.tomtop.application.libraries.constentenum;/** * Packaging picking status enum class * @author Zeng.xiao.yan * @date June 5 1:04:40 PM * @version V1.0 */public enum Usestatusenum {packing_not_use (1),//Not picked packing_has_used (2),// Packing_back_use (3) has been picked up; Called back private Integer usestatus;private usestatusenum (int usestatus) {this.usestatus = Usestatus;} Public Integer Getusestatus () {return usestatus;}}</textarea>(2) code snippet two
   
 
  1. package com.tomtop.application.libraries.constentEnum;
  2. /**
  3. * b2b发票状态枚举类
  4. * @author ZENG.XIAO.YAN
  5. * @date Aug 11, 2017 9:32:04 AM
  6. * @version V1.0
  7. * @qq 1927
  8. */
  9. public enum B2bInvoiceStatusEnum {
  10. /** 待开票 */
  11. WAIT_MAKE_INVOICE(10,"待开票"),
  12. /** 待关务审核发票 */
  13. WAIT_CUSTOMS_AUDIT_INVOICE(20,"待关务审核发票"),
  14. /** 待财务审核发票 */
  15. WAIT_FINANCIAL_AUDIT_INVOICE(30,"待财务审核发票"),
  16. /** 待财务认证发票 */
  17. WAIT_FINANCIAL_AUTHENTICATION_INVOICE(40,"待财务认证发票"),
  18. /** 待税局退税 */
  19. WAIT_TAX_STATION_TAX_REIMBURSEMENT(50,"待税局退税"),
  20. /** 不可退税 */
  21. CAN_NOT_TAX_REIMBURSEMENT(60,"不可退税"),
  22. /** 已退税 */
  23. HAS_TAX_REIMBURSEMENT(70,"已退税");
  24. private Integer status;
  25. private String text;
  26. //构造方法
  27. private B2bInvoiceStatusEnum(Integer status,String text ){
  28. this.status = status;
  29. this.text = text;
  30. }
  31. public Integer getStatus() {
  32. return status;
  33. }
  34. public String getText() {
  35. return text;
  36. }
  37. }
Package <textarea style="display: none">com.tomtop.application.libraries.constentenum;/** * Business-to-Business invoice Status Enumeration class * @author ZENG. XIAO. YAN * @date 9:32:04 AM * @version V1.0 * @qq 1927 */public enum B2binvoicestatusenum {/** to open Ticket */wait_make_invoice (10, "to be Invoiced"),/** to Customs audit invoice */wait_customs_audit_invoice (20, "Pending audit Invoice"),/** pending Financial Review Invoice */wait_financial _audit_invoice (30, "pending financial audit Invoice"),/** pending financial certification invoice */wait_financial_authentication_invoice (40, "pending financial certification Invoices"),/** tax rebate */wait_ Tax_station_tax_reimbursement (50, "tax rebate"),/** non-refundable */can_not_tax_reimbursement (60, "non-refundable"),/** has been refunded */has_tax_ Reimbursement (70, "refunded");p rivate integer status;private String text;//constructor method Private B2binvoicestatusenum (integer status , String text) {this.status = Status;this.text = text;} Public Integer GetStatus () {return status;} Public String GetText () {return text;}}</textarea>Second, the enumeration class in the Code usage (1) code fragment One
 
   
  
  1. // 枚举使用案例01
  2. productPackingRecord.setUseStatus(UseStatusEnum.PACKING_NOT_USE.getUseStatus()); //设为未领用状态
<textarea style="display: none">//enumeration use Case 01productpackingrecord.setusestatus (UseStatusEnum.PACKING_NOT_USE.getUseStatus ());//set to no status</textarea>Note: This method is called Getusestatus to return the value of the enumeration constant. (2) Code fragment two
  
 
  1. // 枚举使用案例02
  2. public Map<String,String> getOptions(){
  3. Map<String,String> options = new LinkedHashMap<>();
  4. B2bInvoiceStatusEnum[] statusEnums = B2bInvoiceStatusEnum.values(); // 获得枚举对象数组
  5. // 遍历枚举对象
  6. for (B2bInvoiceStatusEnum b2bInvoiceStatusEnum : statusEnums) {
  7. // eg:put("10","待开票")
  8. options.put(b2bInvoiceStatusEnum.getStatus().toString(), b2bInvoiceStatusEnum.getText());
  9. }
  10. return options;
  11. }
  12. // 枚举使用案例03
  13. /** (3)保存发票,发票状态设为‘待关务部审核发票‘ */
  14. invoiceInfo.setStatus(B2bInvoiceStatusEnum.WAIT_CUSTOMS_AUDIT_INVOICE.getStatus());
  15. session.save(invoiceInfo);
<textarea style="display: none">//enumeration use case map<string,string> getoptions () {map<string,string> options = new LINKEDHASHMAP&L T;> (); b2binvoicestatusenum[] Statusenums = B2binvoicestatusenum.values ();//Get an array of enumerated objects//iterate over the enumeration object for (B2binvoicestatusenum B2binvoicestatusenum:statusenums) {//Eg:put ("10", "Pending Invoicing") Options.put (B2binvoicestatusenum.getstatus (). ToStrin g (), B2binvoicestatusenum.gettext ());} return options;} Enumerate the use case 03/** (3) to save the invoice, the invoice status is set to ' pending Customs department audit Invoice ' */invoiceinfo.setstatus (b2binvoicestatusenum.wait_customs_audit_ Invoice.getstatus ()); Session.save (Invoiceinfo)</textarea> ;
Reference Link: http://blog.csdn.net/u014527058/article/details/52751488 http://blog.csdn.net/qiyueqing lian/article/details/50738548


To be continued ...

Syntax and usage of enumerations in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.