I have a clas called as MyFunctions
that defines different functions func1
, func2
, etc. Also I have a class Process
that stores the function name assigned to the object of this class:
Process p1 = new Process();
String fName1 = "func1";
p1.setFunctionName(fName1);
Process p2 = new Process();
String fName2 = "func2";
p2.setFunctionName(fName2);
In order to run a proper function, I do the following:
MyFunctions f = new MyFunctions();
if (p.getFunctionName() == "func1") {
output = f.func1(inputdata);
} else if (p.getFunctionName() == "func2") {
output = f.func2(inputdata);
}
I´m not sure that this approach is efficient. Is there any other way to solve this task?
Another question: is it possible to do something like this in JAVA?:
String fName = p.getFunctionName();
output = f."+fName+"(input);