jbe*_*eck 6 c++ java arrays enums
我试图在C++中为以下方便找到Java等价物:
enum {
ANIMAL_CAT = 0,
ANIMAL_RAT,
ANIMAL_BAT, ...
NUM_ANIMALS
};
Animal animals[NUM_ANIMALS];
animals[ANIMAL_CAT].mNumLegs = 4;
animals[ANIMAL_RAT].mNumLegs = 4; ...
Run Code Online (Sandbox Code Playgroud)
我知道这不是世界上最漂亮的东西,但我可以在枚举中的任何地方添加一个新的ANIMAL_xxx,并且所有以下条目都将自动调整.在Java中有一种干净的方法吗?
感谢您的回复,但我可能提到了比我想象的更简单.
我正在开发一个游戏,那里有一个物理,AI引擎等等.我正在尝试整合我需要的每种类型实体的所有信息(有些只有物理表现,有些同时有物理和AI,等...),这样我就可以轻松添加/修改类型(因此,动物是一个不好的例子,因为我可能需要岩石,植物等......).
我正在使用类来存储类型信息,而其他类依赖于该类来查找属性(维度,位图索引,hasAi等等).最后,我正在尝试清理类似的东西以下,同时允许我轻松添加新类型:
class UnitTypeInfo {
UnitTypeInfo() {
mTypes = new TypeInfo[NUM_TYPES];
// and allocate...
// initialize TypeInfos here
mTypes[TYPE_CAT].mType = TYPE_CAT;
mTypes[TYPE_CAT].mMass = 10.0f;
mTypes[TYPE_CAT] ...
mTypes[TYPE_ROCK].mType = TYPE_ROCK;
...
}
public class TypeInfo {
public int mType;
public int mRawResourceHandle;
public float mMass;
public Vector2d mDimensions;
public float mHealth;
public boolean mHasAi;
...
}
public static final int TYPE_CAT = 0;
public static final int TYPE_ROCK = 1;
public static final int TYPE_TREE = 2; ...
public static final int NUM_TYPES = ???; // the last TYPE_ + 1
public TypeInfo mTypes[];
}
Run Code Online (Sandbox Code Playgroud)
现在,这似乎是某种XML实现可能是"正确"的事情,但我不确定如何做到这一点(Java新手).无论如何,其他类可以很容易地使用unitTypeInfo.mTypes [UnitTypeInfo.TYPE_CAT] .mMass(实例化)来查找cat的质量.但是,如果我想在TYPE_CAT定义下面添加一个TYPE_DOG,我必须更新它下面的所有内容(懒惰,我知道,但让我们看看还有更多类型.)
Enums在C++中为这个问题提供了一个简单的解决方案,但是在Java中,我现在能够解决的最简单的解决方案需要:unitTypeInfo.mTypes [UnitTypeInfo.mTypeEnum.TYPE_CAT.ordinal()].mMass - 而我想这个并没有比我已经糟糕的解决方案更糟糕,它确实增加了更多的间接和实际的方法调用(对于大多数来源似乎不鼓励的方法).
还有一件事是我希望能够调用一个开关(类型),这也可能限制了可能的解决方案.
无论如何,我开始意识到必须有一个更好的解决方案来解决这个问题.有什么建议?
"经典"java总是使用"static final int ANIMAL_CAT = 0;" 在这种情况下.
JDK 1.5引入了"类型安全枚举":
http://www.javapractices.com/topic/TopicAction.do?Id=1
你现在可以这样做(一种非常普遍的做法):
enum Quark {
/*
* These are called "enum constants".
* An enum type has no instances other than those defined by its
* enum constants. They are implicitly "public static final".
* Each enum constant corresponds to a call to a constructor.
* When no args follow an enum constant, then the no-argument constructor
* is used to create the corresponding object.
*/
UP,
DOWN,
CHARM,
STRANGE,
BOTTOM,
TOP
}
Run Code Online (Sandbox Code Playgroud)
或者你可以这样做:
/**
* Example 2 - adding a constructor to an enum.
*
* If no constructor is added, then the usual default constructor
* is created by the system, and declarations of the
* enum constants will correspond to calling this default constructor.
*/
public enum Lepton {
//each constant implicity calls a constructor :
ELECTRON(-1, 1.0E-31),
NEUTRINO(0, 0.0);
/*
* This constructor is private.
* Legal to declare a non-private constructor, but not legal
* to use such a constructor outside the enum.
* Can never use "new" with any enum, even inside the enum
* class itself.
*/
private Lepton(int aCharge, double aMass){
//cannot call super ctor here
//calls to "this" ctors allowed
fCharge = aCharge;
fMass = aMass;
}
final int getCharge() {
return fCharge;
}
final double getMass() {
return fMass;
}
private final int fCharge;
private final double fMass;
}
Run Code Online (Sandbox Code Playgroud)
这是官方文件:
http://docs.oracle.com/javase/1.5.0/docs/guide/language/enums.html
为什么不这么简单:
public enum Animal {
CAT(4), RAT(4), BAT(4), CHICKEN(2), ..., ANT(6), SPIDER(8);
// A simple attribute, like any other java class.
// Final to ensure it will never change.
private final int legs;
// Note that this constructor is private.
private Animal(int legs) {
this.legs = legs;
}
// A method, like any other class.
public int getLegs() {
return legs;
}
}
Run Code Online (Sandbox Code Playgroud)
要使用它:
// How to get all the animals in the enum.
Animal[] animals = Animal.values();
// How to get the number of animals.
int numAnimals = animals.length;
// How to get the number of legs of some animal.
int howManyLegsHaveTheAnt = Animal.ANT.getLegs();
int howManyLegsHaveTheFirstAnimal = animals[0].getLegs();
// How to get the name of a given animal.
String theFirstAnimalName = animals[0].name(); // Will return "CAT".
// How to get the position in the enum of some animal.
int antPositionInTheEnum = Animal.ANT.ordinal();
// How to get an animal given its name.
Animal rat = Enum.valueOf(Animal.class, "RAT");
Animal someAnimal = Enum.valueOf(Animal.class, animalName);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5143 次 |
| 最近记录: |