如何避免缺乏扩展枚举的能力? [英] How to circumvent the lack of capability of extending enumerations?

查看:73
本文介绍了如何避免缺乏扩展枚举的能力?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于这个问题的解决方式,我很抱歉。我真的想不出一种好方法来表达它。

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.

我非常想为了使用方法抽象而进行枚举扩展另一个。这将使生活变得如此轻松。 cannot,我不能。那么,有人知道我如何能够实现诸如抽象方法调用之类的功能吗?我尝试了一个界面,但很快了解到您不能使用Enum泛型。

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.

编辑1:
我刚刚找到了,我将对其进行研究,看看是否可以从帮助中得出答案。

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

这篇关于如何避免缺乏扩展枚举的能力?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆