I'm considering the best way to create common functionality between enum values, which implement an abstract method declared in the enum.
For example, I want to share the first line of the methods defined by VALUE1
and VALUE2
:
public enum EnumExample {
VALUE1() {
public void doSomething(String arg) {
doSomethingElse(); // I don't want to repeat this line
calculateFoo();
},
VALUE2() {
public void doSomething(String arg) {
doSomethingElse(); // I don't want to repeat this line
calculateBar();
}
},
VALUE3() {
public void doSomething(String arg) {
doSomethingUnrelated();
}
};
public abstract void doSomething(String arg);
}
Currently, I just repeat the code for each value, but I'm sure there is a better way.
Any help will be much appreciated!
Aucun commentaire:
Enregistrer un commentaire