我有枚举在JAVA代码ERequestTypes
我的枚举包含多于20个元素,每个元素都是我的JAVA代码中的函数的名称。现在我想要做下列的东西,而不是写switch(ERequestTypes) case
,并在案件以这种方式调用函数:来自枚举的调用函数
switch(ERequestTypes a) {
case ERequestTypes.Initialize:
Initialize();
case ERequestTypes.Uninitialize:
Uninitialize();
}
我想用一条线来做到这一点。枚举中的所有函数具有相同的参数并返回相同的int值。我该怎么做?可能会像C++或其他东西一样保留enum中的函数指针。请帮忙 !
class CRequestValue {
/**
* Constructor.
*
* @param aName - Name of the request.
* @param aCode - Code of the request.
*/
public CRequestValue(String aName, int aCode) {
this.mName = aName;
this.mCode = aCode;
}
private String mName;
private int mCode;
public String GetName() {
return this.mName;
}
public int GetCode() {
return this.mCode;
}
} /* class CRequestValue **/
enum ERequestTypes
{
Initialize(new CRequestValue("Initialize", 0)),
Uninitialize(new CRequestValue("Uninitialize", 1)),
GetDeviceInfoList(new CRequestValue("GetDeviceInfoList", 2)),
GetDeviceInfo(new CRequestValue("GetDeviceInfo", 3));
private CRequestValue mRequestValue;
private ERequestTypes(CRequestValue aRequestValue) {
this.mRequestValue = aRequestValue;
}
public String GetName() {
return this.mRequestValue.GetName();
}
public int GetCode() {
return this.mRequestValue.GetCode();
}
} /* enum ERequestTypes **/
为什么要在枚举中安排函数?设计模式的目标是什么? –
Java,而不是JAVA。 – missingfaktor