常量 枚举区别
一直纠结 ,貌似枚举能用的地方.常量都能实现.
public static final int GROUP_MASTER = 1;
public static final int GROUP_VICE_MASTER = 1;
public static final int GROUP_NORMAL = 1;
public static final String GROUP_MASTER_NAME = "会长";
public static final String GROUP_VICE_MASTER_NAME = "副会";
public static final String GROUP_NORMAL_NAME = "会员";
public enum GroupPurviewType {
/**
* 工会会长
*/
MASTER(1,1,"族长"),
/**
* 副会长
*/
VICE_MASTER(4,2,"副会长"),
/**
* 普通会员
*/
NORMAL(8,3,"会员");
private int type = 0;
private int order = 0;
private String name = "";
private GroupPurviewType(int type,int order ,String name)
{
this.type = type;
this.order = order;
this.name = name;
}
public int getValue()
{
return this.type;
}
public String getName()
{
return this.name;
}
public int getOrder()
{
return this.order;
}
public static GroupPurviewType parse(int val)
{
for(GroupPurviewType t : values()){
if(t.getValue() == val)
return t;
}
return NORMAL;
}
/**
* 判断是否是在权限中.
* @param val
* @return
*/
public static boolean exist(int val)
{
for(GroupPurviewType t : values()){
if(t.getValue() == val)
return true;
}
return false;
}
}
很自然,枚举会更加的简洁明了. 而且枚举支持 == 比较 switch case 等操作. 而且可以自己封装方法. 所以个人认为枚举确实好一些.