开发者

How to circumvent the lack of capability of extending enumerations?

开发者 https://www.devze.com 2023-03-10 20:04 出处:网络
I am sorry ahead of time for how this question may turn out. I really can\'t think开发者_如何学Go of a good way to word it.

I am sorry ahead of time for how this question may turn out. I really can't think开发者_如何学Go of a good way to word it.

I would very much like to have an enumeration extend another for the sake of use method abstraction. It would make life so much easier. Alas, I cannot. So, does anyone know how I may be able to implement a feature like an abstract method call? I tried an interface but quickly learned that you can't use an Enum generic.

Edit 1: I just found this, I will look at it and see if I can derive my answer from the help. I will leave this open and unanswered just in case however.


We often use the visitor pattern to allow extending enums with external functionality. Something like

boolean reallyLoveThisDay = dayOfWeek.accept(new DefaultDayOfWeekVisitor<Boolean>()
{
  public Boolean visitMonday(DayOfWeek dayOfWeek) { return false; }
  public Boolean visitDefault(DayOfWeek dayOfWeek) { return true; }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消