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

例说java枚举类型 博客分类: java java枚举 

程序员文章站 2024-03-21 21:30:28
...
import java.util.Calendar;


/**
 *最简普通枚举
 * @author  Walter.Huang
 * @create date 2014-12-18
 */
enum WEEKEND{
	SATURDAY,SUNDAY;
}


/**
 * 带有静态方法的的枚举
 * 
 * @author Walter.Huang
 * @create date 2014-12-18
 */
enum Season {

	SPRING, SUMMER, AUTUMN, WINTER;// 定义四季

	/**
	 * 枚举像普通的类一样可以添加属性和方法,可以为它添加静态和非静态的属性或方法
	 * @param month
	 * @return Season
	 * @author Walter.Huang
	 * @update date 2014-12-18
	 */
	public static Season getSeason(int month) {

		if (month >= 2 && month <= 4)
			return SPRING;
		if (month >= 5 && month <= 7)
			return SUMMER;
		if (month >= 8 && month <= 10)
			return AUTUMN;

		return WINTER;
	}
}


/**
 * 实现带有构造器的枚举
 * @author Walter.Huang
 * @create date 2014-12-18
 */
enum Authority {

	LIST_ARTICLE("admin/article/list.do", 1), 
	UPDATE_ARTICLE("admin/article/update.do", 2),
	DELETE_ARTICLE("admin/article/delete.do", 3);

	Authority(String uri, int code) {
		setUri(uri);
		setCode(code);
	}

	/**
	 * 根据uri获取权限
	 * @param srcUri
	 * @return Authority
	 * @author Walter.Huang
	 * @update date 2014-12-18
	 */
	public static Authority getAuthority(String srcUri){
		for(Authority authority: Authority.values()){
			if(srcUri.indexOf(authority.getUri())>0){
				return authority;
			}
		}	
		return null;
	}
	
	
	private String uri;
	private int code;

	public String getUri() {
		return uri;
	}

	public void setUri(String uri) {
		this.uri = uri;
	}

	public int getCode() {
		return code;
	}

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

}


/**
 * 实现带有抽象方法的枚举 
 * @author  Walter.Huang
 * @create date 2014-12-18
 */
enum OrderState {  
     
	 CANCEL {//取消
    	 public String getName(){return "已取消";}
    	 public int getStatus(){return -1;}
     },  

     WAITCONFIRM {//待审核
    	 public String getName(){return "待审核";}
    	 public int getStatus(){return 0;}
     },
     
     WAITPAYMENT {//等待付款
    	 public String getName(){return "等待付款";}
    	 public int getStatus(){return 1;}
     },
     
     ADMEASUREPRODUCT {//正在配货
    	 public String getName(){return "正在配货";}
    	 public int getStatus(){return 2;}
     },
     
     WAITDELIVER {//等待发货
    	 public String getName(){return "等待发货";}
    	 public int getStatus(){return 3;}
     }, 
     
     DELIVERED {//已发货
    	 public String getName(){return "已发货";}
    	 public int getStatus(){return 4;}
     },  
 
     RECEIVED {//已收货
    	 public String getName(){return "已收货";}
    	 public int getStatus(){return 5;}
     };
     
     public abstract String getName();
     public abstract int getStatus();
     
     
     /**
      * 根据状态码获取订单状态
      * @param status
      * @return OrderState
      * @author Walter.Huang
      * @update date 2014-12-18
      */
     public static OrderState getOrderStateByStatus(int status){
    	 for(OrderState orderState:OrderState.values()){
    		 if(orderState.getStatus()==status){
    			 return orderState;
    		 }
    	 }
    	 return null;
     }
     
     
     /**
      * 根据状态码获取订单状态名
      * @param status
      * @return String
      * @author Walter.Huang
      * @update date 2014-12-18
      */
     public static String getNameByStatus(int status){
    	 for(OrderState orderState:OrderState.values()){
    		 if(orderState.getStatus()==status){
    			 return orderState.getName();
    		 }
    	 }
    	 return "";
     }
     
 }

/**
 *测试类 
 * @author  Walter.Huang
 * @create date 2014-12-18
 */
public class AllEnum {
	public static void main(String[] args) {
		
		/*简单的枚举类一般常用witch...case语句做判断*/
		WEEKEND aday=WEEKEND.SATURDAY;
		switch(aday){
			case SATURDAY:
				System.out.println("今天是周六,可以睡懒觉!");
				break;
			case SUNDAY:
				System.out.println("今天是周日,可以去爬山!");
				break;
			default:
				System.out.println("今天要工作,一起加油哦!");
		}
		/****************/
		
		
		/*根据月份获取当前的季节*/
		Calendar calendar = Calendar.getInstance();
		int month=calendar.get(Calendar.MONTH)+1;//获取当前是年中的第几月
		
		Season aSeason=Season.getSeason(month);
		switch(aSeason){
			case SPRING:
				System.out.println(month+"月,春天,希望!");
				break;
			case SUMMER:
				System.out.println(month+"月,夏天,热情!");
				break;
			case AUTUMN:
				System.out.println(month+"月,秋天,收获!");
				break;
			case WINTER:
				System.out.println(month+"月,冬天,积蓄!");
				break;
		}
		/****************/
		
		
		/*根据根据拦截的uri获取权限的代码*/
		String requestUri="http://www.walter.com/admin/article/update.do?flag=100";
		Authority authority = Authority.getAuthority(requestUri);
		if(authority!=null){	
			System.out.println("权限uri规则是:"+authority.getUri()+" 权限的代码是:"+authority.getCode());
		}else{
			System.out.println("改uri没有添加加权限的控制,允许直接访问");
		}
		/****************/
		
		
		/*订单状态枚举方式描述*/
		OrderState orderState=OrderState.RECEIVED;
		System.out.println("订单状态是:"+orderState.getName()+" 状态代码是:"+orderState.getStatus());
		
		int status = 2;
		orderState=OrderState.getOrderStateByStatus(status);
		if(orderState!=null){	
			System.out.println("状态代码是"+status+"的订单状态是:"+orderState.getName());
		}else{
			System.out.println("订单状态不存在");
		}
		/****************/
	}
	
}

 运行结果:

 

今天是周六,可以睡懒觉!

12月,冬天,积蓄!

权限uri规则是:admin/article/update.do 权限的代码是:2

订单状态是:已收货 状态代码是:5

状态代码是2的订单状态是:正在配货

 

 

相关标签: java 枚举