我刚刚发现Java允许枚举实现接口。它的一个好的用例是什么?
当前回答
有一个我经常用的箱子。我有一个IdUtil类的静态方法与对象一起工作,实现一个非常简单的可识别接口:
public interface Identifiable<K> {
K getId();
}
public abstract class IdUtil {
public static <T extends Enum<T> & Identifiable<S>, S> T get(Class<T> type, S id) {
for (T t : type.getEnumConstants()) {
if (Util.equals(t.getId(), id)) {
return t;
}
}
return null;
}
public static <T extends Enum<T> & Identifiable<S>, S extends Comparable<? super S>> List<T> getLower(T en) {
List<T> list = new ArrayList<>();
for (T t : en.getDeclaringClass().getEnumConstants()) {
if (t.getId().compareTo(en.getId()) < 0) {
list.add(t);
}
}
return list;
}
}
如果我创建一个可识别的enum:
public enum MyEnum implements Identifiable<Integer> {
FIRST(1), SECOND(2);
private int id;
private MyEnum(int id) {
this.id = id;
}
public Integer getId() {
return id;
}
}
然后我可以通过它的id得到它:
MyEnum e = IdUtil.get(MyEnum.class, 1);
其他回答
枚举不只是表示被动的集合(例如颜色)。它们可以表示具有功能的更复杂的对象,因此你可能想要在这些对象上添加更多的功能——例如,你可能有诸如Printable、Reportable等接口,以及支持这些接口的组件。
我在一个接口中使用了一个内部枚举来描述一个策略来保持实例控制(每个策略都是一个Singleton)。
public interface VectorizeStrategy {
/**
* Keep instance control from here.
*
* Concrete classes constructors should be package private.
*/
enum ConcreteStrategy implements VectorizeStrategy {
DEFAULT (new VectorizeImpl());
private final VectorizeStrategy INSTANCE;
ConcreteStrategy(VectorizeStrategy concreteStrategy) {
INSTANCE = concreteStrategy;
}
@Override
public VectorImageGridIntersections processImage(MarvinImage img) {
return INSTANCE.processImage(img);
}
}
/**
* Should perform edge Detection in order to have lines, that can be vectorized.
*
* @param img An Image suitable for edge detection.
*
* @return the VectorImageGridIntersections representing img's vectors
* intersections with the grids.
*/
VectorImageGridIntersections processImage(MarvinImage img);
}
枚举实现该策略的事实便于允许枚举类充当其所包含的Instance的代理。其中也实现了接口。
它是一种strategyEnumProxy:P,客户端代码看起来像这样:
VectorizeStrategy.ConcreteStrategy.DEFAULT.processImage(img);
如果它没有实现接口,它将是:
VectorizeStrategy.ConcreteStrategy.DEFAULT.getInstance().processImage(img);
例如,如果您有一个Logger enum。然后,在接口中应该有日志记录器方法,如debug、info、warning和error。它使您的代码松散耦合。
有一个我经常用的箱子。我有一个IdUtil类的静态方法与对象一起工作,实现一个非常简单的可识别接口:
public interface Identifiable<K> {
K getId();
}
public abstract class IdUtil {
public static <T extends Enum<T> & Identifiable<S>, S> T get(Class<T> type, S id) {
for (T t : type.getEnumConstants()) {
if (Util.equals(t.getId(), id)) {
return t;
}
}
return null;
}
public static <T extends Enum<T> & Identifiable<S>, S extends Comparable<? super S>> List<T> getLower(T en) {
List<T> list = new ArrayList<>();
for (T t : en.getDeclaringClass().getEnumConstants()) {
if (t.getId().compareTo(en.getId()) < 0) {
list.add(t);
}
}
return list;
}
}
如果我创建一个可识别的enum:
public enum MyEnum implements Identifiable<Integer> {
FIRST(1), SECOND(2);
private int id;
private MyEnum(int id) {
this.id = id;
}
public Integer getId() {
return id;
}
}
然后我可以通过它的id得到它:
MyEnum e = IdUtil.get(MyEnum.class, 1);
这是我的理由……
我用Enum的值填充了一个JavaFX组合框。我有一个接口,可识别(指定一个方法:identify),它允许我指定任何对象如何为我的应用程序标识自己,以便进行搜索。这个接口使我能够扫描任何类型的对象(对象可能用于标识的任何字段)的列表以进行标识匹配。
我想在我的组合框列表中找到一个匹配的标识值。为了在包含Enum值的组合框上使用此功能,我必须能够在我的Enum中实现可识别接口(碰巧,在Enum的情况下实现是微不足道的)。
推荐文章
- 在流中使用Java 8 foreach循环移动到下一项
- 访问限制:'Application'类型不是API(必需库rt.jar的限制)
- 用Java计算两个日期之间的天数
- 如何配置slf4j-simple
- 在Jar文件中运行类
- 带参数的可运行?
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- 我可以在Java中设置enum起始值吗?
- Java中的回调函数
- c#和Java中的泛型有什么不同?和模板在c++ ?
- 在Java中,流相对于循环的优势是什么?
- Jersey在未找到InjectionManagerFactory时停止工作
- 在Java流是peek真的只是调试?
- Recyclerview不调用onCreateViewHolder
- 将JSON字符串转换为HashMap