欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

利用枚举,得到类型的字段

程序员文章站 2022-04-23 11:50:00
...

枚举类:

package com.yzf.cloud.funds.model.enums;

public enum ReceiptTypeEnum {

	OUTPUTINVOICE("销项发票", 1), INCOMINVOICE("进项发票", 2), OTHERINVOICE("其他发票", 3), BANKJOURNAL("银行日记账",
			4), CASHJOURNAL("现金日记账", 5), CERTIFIEDINVOICE("认证发票", 6);

	private String desc;
	private int value;

	private ReceiptTypeEnum(String desc, int value) {
		this.desc = desc;
		this.value = value;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public int getValue() {
		return value;
	}

	public void setValue(int value) {
		this.value = value;
	}

	public static String getDesc(int value) {
		switch (value) {
		case 1:
			return ReceiptTypeEnum.OUTPUTINVOICE.getDesc();
		case 2:
			return ReceiptTypeEnum.INCOMINVOICE.getDesc();
		case 3:
			return ReceiptTypeEnum.OTHERINVOICE.getDesc();
		case 4:
			return ReceiptTypeEnum.BANKJOURNAL.getDesc();
		case 5:
			return ReceiptTypeEnum.CASHJOURNAL.getDesc();
		case 6:
			return ReceiptTypeEnum.CERTIFIEDINVOICE.getDesc();
		}
		return "";
	}
}

获取对应的 值:

ReceiptTypeEnum.OUTPUTINVOICE.getValue();//得到序号
ReceiptTypeEnum.OUTPUTINVOICE.getDesc();//得到字段
ReceiptTypeEnum.getDesc(receiptType);//得到序号对应的字段