Java中枚举的写法和用法详解编程语言

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

一、枚举的写法

        关于枚举的写法,网上好多这方面的知识。这里直接贴一个我自己写的枚举类的代码。
(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; 
    } 
   
}
27

 

1

package com.tomtop.application.libraries.constentEnum;

2

 

3

/**     

4

 * 包材领用状态枚举类 

5

 * @author  zeng.xiao.yan    

6

 * @date  Jun 5, 2017 1:04:40 PM  

7

 * @version  V1.0    

8

 */

9

10

public enum UseStatusEnum {

11

  

12

    PACKING_NOT_USE(1),  // 未领用  

13

    PACKING_HAS_USED(2),  // 已领用  

14

    PACKING_BACK_USE(3);  // 已打回

15

    

16

  

17

    private Integer useStatus;

18

    

19

    private UseStatusEnum(int useStatus) {

20

        this.useStatus = useStatus;

21

    }

22

  

23

    public Integer getUseStatus() {

24

        return useStatus;

25

    }

26

  

27

}

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; 
	} 
}
22

 

1

package com.tomtop.application.libraries.constentEnum;

2

3

/**     

4

 * 包材领用状态枚举类

5

 * @author  zeng.xiao.yan    

6

 * @date  Jun 5, 2017 1:04:40 PM  

7

 * @version  V1.0    

8

 */

9

10

public enum UseStatusEnum {

11

    PACKING_NOT_USE(1),  // 未领用

12

    PACKING_HAS_USED(2),  // 已领用

13

    PACKING_BACK_USE(3);  // 已打回

14

    

15

    private Integer useStatus;

16

    private UseStatusEnum(int useStatus) {

17

        this.useStatus = useStatus;

18

    }

19

    public Integer getUseStatus() {

20

        return useStatus;

21

    }

22

}

(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; 
    } 
 
}
45

 

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

10

11

public enum B2bInvoiceStatusEnum {

12

13

    /** 待开票 */

14

    WAIT_MAKE_INVOICE(10,"待开票"),

15

    /** 待关务审核发票 */

16

    WAIT_CUSTOMS_AUDIT_INVOICE(20,"待关务审核发票"),

17

    /** 待财务审核发票 */

18

    WAIT_FINANCIAL_AUDIT_INVOICE(30,"待财务审核发票"),

19

    /** 待财务认证发票 */

20

    WAIT_FINANCIAL_AUTHENTICATION_INVOICE(40,"待财务认证发票"),

21

    /** 待税局退税 */

22

    WAIT_TAX_STATION_TAX_REIMBURSEMENT(50,"待税局退税"),

23

    /** 不可退税 */

24

    CAN_NOT_TAX_REIMBURSEMENT(60,"不可退税"),

25

    /** 已退税 */

26

    HAS_TAX_REIMBURSEMENT(70,"已退税");

27

28

    private Integer status;

29

    private String text;

30

    

31

    //构造方法

32

    private B2bInvoiceStatusEnum(Integer status,String text ){

33

        this.status = status;

34

        this.text = text;

35

    }

36

37

    public Integer getStatus() {

38

        return status;

39

    }

40

    

41

    public String getText() {

42

        return text;

43

    }

44

45

}

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; 
	} 
	 
}
43

 

1

package com.tomtop.application.libraries.constentEnum;

2

3

/**     

4

 * b2b发票状态枚举类 

5

 * @author  ZENG.XIAO.YAN    

6

 * @date    Aug 11, 2017 9:32:04 AM  

7

 * @version V1.0    

8

 * @qq      1927 

9

 */

10

11

public enum B2bInvoiceStatusEnum {

12

    /** 待开票 */

13

    WAIT_MAKE_INVOICE(10,"待开票"),

14

    /** 待关务审核发票 */

15

    WAIT_CUSTOMS_AUDIT_INVOICE(20,"待关务审核发票"),

16

    /** 待财务审核发票 */

17

    WAIT_FINANCIAL_AUDIT_INVOICE(30,"待财务审核发票"),

18

    /** 待财务认证发票 */

19

    WAIT_FINANCIAL_AUTHENTICATION_INVOICE(40,"待财务认证发票"),

20

    /** 待税局退税 */

21

    WAIT_TAX_STATION_TAX_REIMBURSEMENT(50,"待税局退税"),

22

    /** 不可退税 */

23

    CAN_NOT_TAX_REIMBURSEMENT(60,"不可退税"),

24

    /** 已退税 */

25

    HAS_TAX_REIMBURSEMENT(70,"已退税");

26

    private Integer status;

27

    private String text;

28

    

29

    //构造方法

30

    private B2bInvoiceStatusEnum(Integer status,String text ){

31

        this.status = status;

32

        this.text = text;

33

    }

34

35

    public Integer getStatus() {

36

        return status;

37

    }

38

39

    public String getText() {

40

        return text;

41

    }

42

    

43

}

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

(1)代码片段一
//  枚举使用案例01 
productPackingRecord.setUseStatus(UseStatusEnum.PACKING_NOT_USE.getUseStatus()); //设为未领用状态
2

 

1

//  枚举使用案例01

2

productPackingRecord.setUseStatus(UseStatusEnum.PACKING_NOT_USE.getUseStatus()); //设为未领用状态

//  枚举使用案例01 
productPackingRecord.setUseStatus(UseStatusEnum.PACKING_NOT_USE.getUseStatus()); //设为未领用状态
2

 

1

//  枚举使用案例01

2

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);
17

 

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

  

13

  

14

    //  枚举使用案例03

15

    /** (3)保存发票,发票状态设为'待关务部审核发票' */

16

    invoiceInfo.setStatus(B2bInvoiceStatusEnum.WAIT_CUSTOMS_AUDIT_INVOICE.getStatus());

17

    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);
x
 

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

13

    //  枚举使用案例03

14

    /** (3)保存发票,发票状态设为'待关务部审核发票' */

15

    invoiceInfo.setStatus(B2bInvoiceStatusEnum.WAIT_CUSTOMS_AUDIT_INVOICE.getStatus());

16

    session.save(invoiceInfo);

            参考链接:
http://blog.csdn.net/u014527058/article/details/52751488
                             
http://blog.csdn.net/qiyueqinglian/article/details/50738548

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

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/15528.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论