gpt4 book ai didi

java - 如何用 Java 中的枚举替换旧版标志和常量按位运算?

转载 作者:行者123 更新时间:2023-11-29 08:14:15 25 4
gpt4 key购买 nike

问候,

我来到了 C++ 中的一个遗留代码,它使用一个标志,然后根据完成的读取更新标志的状态:

主文件:

#define VINCULACION                 0x00000004L
#define DET_VINCULACION 0x00000008L

long unsigned FlagRead ;

int formerMethod(){
if ((FlagRead & VINCULACION)==0) ReadVinculacion();
//... DO MORE
}

int ReadVinculacion(){
//.. Do DB operations to read Vinculacion variables.
FlagRead|=VINCULACION;
return 1;
}

//.. Same similar methods to ensure reading DET_VINCULACION but not doing it twice.

现在使用 Java 进行开发,我没有将常量与整数或长整型一起用作使用枚举的良好做法。

是否有一种性能明智且可靠的方法可以在 java 下使用枚举来完成相同的任务?

谢谢!

最佳答案

看看使用 EnumSet取代 C++ 代码中的 FlagRead 变量:

Enum sets are represented internally as bit vectors. This representation is extremely compact and efficient. The space and time performance of this class should be good enough to allow its use as a high-quality, typesafe alternative to traditional int-based "bit flags."

然后您可以使用 set.contains(YourEnum.SOME_VALUE)

测试是否应该执行某些操作

关于java - 如何用 Java 中的枚举替换旧版标志和常量按位运算?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5784157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com