I have enumeration in JAVA code ERequestTypes my enumeration contains more then 20 elements, every element is a name of a function in my JAVA code. Now I want do following thing instead of writing switch(ERequestTypes) case and in the cases calling function in this way:
switch(ERequestTypes a) {
case ERequestTypes.Initialize:
Initialize();
case ERequestTypes.Uninitialize:
Uninitialize();
}
I want to do it with one line. All the functions in the enum have same argument and return same int value. How I can do that ? may be keep pointers of functions in enum like in C++ or something else. Please help !
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 **/
You’re looking for reflection.
Note that the Class.forName is not required if you already know what class the methods are in.