JAVA中enum使用方法

注意查看结果,STATUSCODE尤为重要

package com.enumtest;

public enum RESULTMAP {

	SUCCESS(200,"返回正确结果"),INPUT_PARAM_ERROR(301,"入参有误");

	
	private int code;
	
	private String desc;

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}
	
	private RESULTMAP(int code,String desc){
		this.code = code;
		this.desc = desc; 
	}
}

package com.enumtest;

public enum SEAZON {
	SPRING("春天"),SUMMAR("夏天");
	
	private String seazon;
	
	public String getSeazon() {
		return seazon;
	}

	public void setSeazon(String seazon) {
		this.seazon = seazon;
	}

	private SEAZON(String seazon){
		this.seazon = seazon;
	}
}

package com.enumtest;

public enum WEEKDAY {
	MONDAY(new Integer(1)),TUESDAY(new Integer(1));
	
	private Integer day;
	
	public Integer getDay() {
		return day;
	}

	public void setDay(Integer day) {
		this.day = day;
	}

	private WEEKDAY(Integer day) {
		this.day = day;
	}
}

package com.enumtest;

public enum STATUSCODE {
	SUCCESS("200","返回正确结果"),INPUT_PARAM_ERROR("301","入参有误");

	
	private String code;
	
	private String desc;

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}
	
	private STATUSCODE(String code,String desc){
		this.code = code;
		this.desc = desc; 
	}
}

package com.enumtest;

public class MainTest {

	public static void main(String[] args) {
		WEEKDAY monday = WEEKDAY.MONDAY;
		System.out.println(monday.getDay());
		System.out.println(monday.getDay().getClass());
		System.out.println("=====================================");
		SEAZON seazon = SEAZON.SPRING;
		System.out.println(seazon);
		System.out.println(seazon.getSeazon().getClass());
		System.out.println("=====================================");
		RESULTMAP res = RESULTMAP.SUCCESS;
		System.out.println(res);
		System.out.println(res.getCode());
		System.out.println(res.getDesc());
		System.out.println(res.getClass());
		System.out.println("=====================================");
		STATUSCODE resSS = STATUSCODE.SUCCESS;
		System.out.println(resSS.getCode());
		System.out.println(resSS.getDesc());
		System.out.println(resSS.getClass());
		
	}

}


运行结果:


1
class java.lang.Integer
=====================================
SPRING
class java.lang.String
=====================================
SUCCESS
200
返回正确结果
class com.enumtest.RESULTMAP
=====================================
200
返回正确结果
class com.enumtest.STATUSCODE


 
  











你可能感兴趣的:(JAVA)