java枚举类实例

enum实例

package test;

import org.junit.Test;

public class EnumTest {

	@Test
	public void test01(){
		System.out.println("===code:"+TestEnum.ERROR.getCode());
		System.out.println("===msg:"+TestEnum.ERROR.getMsg());
		
		System.out.println(TestEnum.MISSING_PARAMETER.getMsg("hello world"));
		
		TestEnum[] values = TestEnum.values();
		for (TestEnum value:TestEnum.values()) {
			System.out.println(value);
		}
		System.out.println("1:"+TestEnum.values());
	}
}

enum TestEnum{
	MISSING_PARAMETER("1020", "缺失必要参数:%s"),
	ERROR("9999","系统异常");
	//必须有构造方法
	private String code;
	private String msg;
	private TestEnum(String code, String msg) {
		this.code = code;
		this.msg = msg;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public String getMsg(Object... orgs) {
		if(msg == null) return msg;
		return String.format(msg, orgs);
	}
	
}

猜你喜欢

转载自cylu.iteye.com/blog/2309545
今日推荐