Lambda
------
You can use lambdas to save them in a container to call them.
@FunctionalInterface
public interface Method {
double execute(int number);
}
And contain methods in a container, **You can use hashmap to invoke them by strings or array to invoke them by numbers**.
public class Shape {
private final static double PI = 3.14;
private Method[] methods = {
number -> {
return number * number;
},
number -> {
return PI * number * number;
},
};
public double run(int methodIndex, int number) {
return methods[methodIndex].execute(aNumber);
}
}