How to circumvent the lack of capability of extending enumerations?

穿精又带淫゛_ 提交于 2019-12-11 13:41:36

问题


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:


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; }
});


来源:https://stackoverflow.com/questions/6233213/how-to-circumvent-the-lack-of-capability-of-extending-enumerations

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!