How to circumvent the lack of capability of extend

2019-08-08 20:27发布

I am sorry ahead of time for how this question may turn out. I really can't think 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.

1条回答
萌系小妹纸
2楼-- · 2019-08-08 20:50

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; }
});
查看更多
登录 后发表回答