您的位置:首页 > 编程语言 > Java开发

谈谈枚举的新用法——java

2016-07-06 15:37 555 查看

问题的由来

前段时间改游戏buff功能,干了一件愚蠢的事情,那就是把枚举和运算集合在一起,然后运行一段时间后buff就出现各种问题,我当时懵逼了!

事情是这样的,做过游戏的都知道,buff,需要分类型,且包含是否需要切换场景删除,下线删除,死亡删除等信息,好吧我就把这个做到一起了用一个字段标识!

问题就出来了;

/**
*
* <br>
* author 失足程序员<br>
* mail 492794628@qq.com<br>
* phone 13882122019<br>
*/
public class EnumTest {

private static final Logger log = Logger.getLogger(EnumTest.class);

public enum BuffType {

Null(0, "空buff"),
DieDel(62, "死亡删除"),
ChangeMapDel(63, "切换场景删除");

int index;
long value;
String msgString;

BuffType(int index, String msg) {
this.index = index;
this.value = 1L << index;
msgString = msg;
}

public int getIndex() {
return index;
}

public String getMsgString() {
return msgString;
}

public static BuffType getBuffType(int index) {
BuffType[] values = BuffType.values();
for (BuffType buffType : values) {
if (buffType.getIndex() == index) {
return buffType;
}
}
return null;
}

/**
* 是否包含一个状态
*
* @param tvalue
* @return
*/
public boolean hasFlag(BuffType tvalue) {
return (value & tvalue.value) != 0;
}

/**
* 增加一个状态
*
* @param buffType
*/
public void addStatus(BuffType buffType) {
value = value | buffType.value;
}

/**
* 移除一个状态
*
* @param buffType
*/
public void removeStatus(BuffType buffType) {
value = value & (~buffType.value);
}
}

public static void main(String[] args) {
BuffType buffType = BuffType.Null;

buffType.addStatus(BuffType.DieDel);

}
}

晃眼一看好像没什么问题对吧??当然你已经有过这样一次上当经历的宁说!

解密

当时情况是这样的,我修改完buff功能模块以后我就让策划测试代码功能,我继续改其他功能模块,然后策划告诉我不对,
我准备调试的时候可是代码已经不是源代码了,需要重新启动程序才能调试,
好吧我重启完成,测试告诉我对了,好吧我又继续该功能模块,过了半小时策划再次告诉我不对了,我又一次的需要重启才能调试,
好吧就这样陷入死循环,一直到晚上吃饭我。、我直接说改了一下午代码走出去活动活动吃饭回来继续看,
回来后我就不再修改代码,就等测试这个功能;这下发现了情况,

使用流程

public static void main(String[] args) {
BuffType buffType = BuffType.Null;

buffType.addStatus(BuffType.DieDel);
if (buffType.hasFlag(BuffType.DieDel)) {
System.out.println("下线需要删除buff");
}

buffType = BuffType.Null;

if (buffType.hasFlag(BuffType.DieDel)) {
System.out.println("下线需要删除buff");
}
}

接下来看看输出结果:

问题来了为什么我明明是新来的一个赋值对象为啥还是包含状态???

我想了一下,突然回过神来,枚举是静态常量啊,我的任何修改都会导致所有引用全部修改,

好吧,既然找到问题了就来修改吧。

再次阐述一下,我这里需要枚举的原因,一个是为了策划配置方便,他们只要1,2,3这种形式

而我需要位移运算,保存状态值,并且还需要枚举类型的文字描述,提供给运维,运营和策划看的。

修改后如何使用

因为我个人喜欢比较喜欢枚举,加上能定义枚举的方便些;

接下来就修改,修改,

/**
*
* <br>
* author 失足程序员<br>
* mail 492794628@qq.com<br>
* phone 13882122019<br>
*/
public class EnumTest {

private static final Logger log = Logger.getLogger(EnumTest.class);

private long tVlaue = 0;

public enum BuffType {

DieDel(62, "死亡删除"),
ChangeMapDel(63, "切换场景删除");

int index;
long value;
String msgString;

BuffType(int index, String msg) {
this.index = index;
this.value = 1L << index;
msgString = msg;
}

public int getIndex() {
return index;
}

public String getMsgString() {
return msgString;
}
}

/**
* 根据策划配置还原枚举
*
* @param index
* @return
*/
public static BuffType getBuffType(int index) {
BuffType[] values = BuffType.values();
for (BuffType buffType : values) {
if (buffType.getIndex() == index) {
return buffType;
}
}
return null;
}

/**
* 是否包含一个状态
*
* @param tvalue
* @return
*/
public boolean hasFlag(BuffType tvalue) {
return (tVlaue & tvalue.value) != 0;
}

/**
* 增加一个状态
*
* @param buffType
*/
public void addStatus(BuffType buffType) {
tVlaue = tVlaue | buffType.value;
}

/**
* 移除一个状态
*
* @param buffType
*/
public void removeStatus(BuffType buffType) {
tVlaue = tVlaue & (~buffType.value);
}

public static void main(String[] args) {
EnumTest buffType = new EnumTest();

buffType.addStatus(BuffType.DieDel);
if (buffType.hasFlag(BuffType.DieDel)) {
System.out.println("包含:" + BuffType.DieDel.getMsgString());
} else {
System.out.println("不包含:" + BuffType.DieDel.getMsgString());
}

buffType = new EnumTest();

if (buffType.hasFlag(BuffType.DieDel)) {
System.out.println("包含:" + BuffType.DieDel.getMsgString());
} else {
System.out.println("不包含:" + BuffType.DieDel.getMsgString());
}
}
}

结果:

我知道这篇文字对大神没什么用,希望对新手有所帮助,同时也记录一次犯二的我!

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: