I want to have a mapping between datatype and the methodname. As for a particular datatype a different method would be called. So, I want to store that in form of enum. How can I achieve it. For example for CHECKBOX type I want to call a method with the name checkboxsimilaity, how should I store store and access in the structure below.
public enum MethodNames{
//CHECHBOX with the value checkboxsimilaity (of type method)
public final Method name;
}
You may try to use functional interfaces to associate some functionality with enum values:
public enum ElementType {
CHECKBOX(() -> {
System.out.println("Select checkbox here or call needed method");
return "selected";
}),
TEXT_FIELD(() -> {
System.out.println("Type something into text field or call needed method");
return "typed";
});
private Supplier<String> action;
ElementType(Supplier<String> action) {
this.action = action;
}
public String executeAssociatedAction() {
return action.get();
}
}
And you can do almost whatever you want there. The invoking will than be:
public static void main(String[] args) {
ElementType.CHECKBOX.executeAssociatedAction();
}
Alternatively, you can have methods implemented somewhere else, have enum with a string as a parameter (which will be the method name) and use reflection to call a method with the corresponding name from the corresponding class.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.