我正试图将一些C++代码翻译成Java。我正在寻找最好的方式来模拟这种类型的C++模式的在Java中 - 我想枚举可能是答案,但我接受任何用Java Enum模拟C++枚举整数
C++代码:
typedef UInt32 Type;
enum { UNKNOWN, QUIT, SYSTEM, TIMER, LAST }
...
Type newType = UNKNOWN;
Type nextType = LAST + 1;
...
// "Register" the new type
newType = nextType;
nextType++;
...
switch (newType) {
case UNKNOWN:
case QUIT:
...
case LAST:
// Ignore unset or predefined types
default:
// Some type other than the predefined. Do something special
基本上我” m寻找一种方法来“扩展”Java枚举的值。
enum Type { UNKNOWN, QUIT, SYSTEM, TIMER, LAST }
不退出削减它。
我喜欢为强打字制作新对象的想法。
再次,我正在寻找最佳模式在这里使用。我可以很容易地通过一些公共静态final int UNKNOWN来浮动,等等。
您是否承诺使用switch语句?还是会有其他派遣机制呢? – 2010-10-05 22:32:30
@Burleigh Bear:代码中的开关只是确定它是否是未知的。所以是的,可能任何派遣将工作 – Shaun 2010-10-06 00:12:15