Is it possible to copy this functionality of offering an abstract method in an enum that inner enum constants must override and provide functionality to?
public enum Logic {
PAY_DAY {
@Override
public void acceptPlayer(Player player) {
// Perform logic
}
},
COLLECT_CASH {
@Override
public void acceptPlayer(Player player) {
// Perform logic
}
}
,
ETC_ETC {
@Override
public void acceptPlayer(Player player) {
// Perform logic
}
};
public abstract void acceptPlayer(Player player);
}
If it can’t::
Could you provide a way which I can implement lots of specific logic in a similar manner?
Edit :: I know that enums in C# are not really ‘objects’ like they are in Java, but I wish to perform similar logic.
Edit :: To clarify, I do not want to provide concrete classes for each specific bit of logic. IE, creating an interface acceptPlayer and creating many new classes is not appropiate
Here’s one option – not using enums, but something similar-ish…
You say that you don’t want to provide a concrete class for each bit of logic – but that’s basically what you’d be doing in Java anyway, it’s just that the class would be slightly hidden from you.
Here’s an alternative approach using delegates for the varying behaviour:
In both cases, you still get a fixed set of values – but you won’t be able to switch on them. You could potentially have a separate “real” enum, but that would be a bit messy.