如何启用枚举inheritance

我正在编写一个库,它具有一组预定义的枚举值。 比方说,我的枚举如下所示。

public enum EnumClass { FIRST("first"), SECOND("second"), THIRD("third"); private String httpMethodType; } 

现在,使用此库的客户端可能需要添加更多值。 假设客户端需要添加CUSTOM_FIRSTCUSTOM_SECOND 。 这不会覆盖任何现有值,但会使枚举具有5个值。

在此之后,我应该可以使用 有5种不变的可能性。

实现这一目标的最佳方法是什么?

你不能让enum扩展另一个enum ,你不能通过inheritance“添加”现有enum值。

但是, enum可以实现interface

我要做的是让原始的enum实现一个标记interface (即没有方法声明),然后你的客户端可以创建自己的实现相同interfaceenum

然后你的enum值将通过它们的公共interface引用。

为了增强需求,你可以让你的接口声明相关的方法,例如在你的情况下, public String getHTTPMethodType();

这将迫使实现enum为该方法提供实现。

此设置与适当的API文档相结合,有助于以相对受控的方式添加function。

自包含的例子 (不介意这里的懒人名字)

 package test; import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { List blah = new ArrayList<>(); blah.add(LibraryEnum.FIRST); blah.add(ClientEnum.BLABLABLA); for (HTTPMethodConvertible element: blah) { System.out.println(element.getHTTPMethodType()); } } static interface HTTPMethodConvertible { public String getHTTPMethodType(); } static enum LibraryEnum implements HTTPMethodConvertible { FIRST("first"), SECOND("second"), THIRD("third"); String httpMethodType; LibraryEnum(String s) { httpMethodType = s; } public String getHTTPMethodType() { return httpMethodType; } } static enum ClientEnum implements HTTPMethodConvertible { FOO("GET"),BAR("PUT"),BLAH("OPTIONS"),MEH("DELETE"),BLABLABLA("POST"); String httpMethodType; ClientEnum(String s){ httpMethodType = s; } public String getHTTPMethodType() { return httpMethodType; } } } 

产量

 first POST 

枚举不可扩展。 简单地解决你的问题

  • 在课堂上转动枚举
  • 为预定义类型创建常量
  • 如果您想要替换Enum.valueOf :在静态地图中跟踪该类的所有实例

例如:

 public class MyType { private static final HashMap map = new HashMap<>(); private String name; private String httpMethodType; // replacement for Enum.valueOf public static MyType valueOf(String name) { return map.get(name); } public MyType(String name, String httpMethodType) { this.name = name; this.httpMethodType = httpMethodType; map.put(name, this); } // accessors public String name() { return name; } public String httpMethodType() { return httpMethodType; } // predefined constants public static final MyType FIRST = new MyType("FIRST", "first"); public static final MyType SECOND = new MyType("SECOND", "second"); ... } 

我们通过这种方式修复了枚举inheritance问题,希望它有所帮助

我们的应用程序有几个类,每个类都有很少的子视图(嵌套视图),为了能够在childViews之间导航并保存currentChildview,我们将它们保存为每个类中的枚举。 但是我们不得不在每个枚举中复制粘贴,一些常见的function,如next,previous等。 为了避免我们需要BaseEnum,我们使用interface作为基本枚举:

 public interface IBaseEnum { IBaseEnum[] getList(); int getIndex(); class Utils{ public IBaseEnum next(IBaseEnum enumItem, boolean isCycling){ int index = enumItem.getIndex(); IBaseEnum[] list = enumItem.getList(); if (index + 1 < list.length) { return list[index + 1]; } else if(isCycling) return list[0]; else return null; } public IBaseEnum previous(IBaseEnum enumItem, boolean isCycling) { int index = enumItem.getIndex(); IBaseEnum[] list = enumItem.getList(); IBaseEnum previous; if (index - 1 >= 0) { previous = list[index - 1]; } else { if (isCycling) previous = list[list.length - 1]; else previous = null; } return previous; } } } 

这就是我们使用它的方式

 enum ColorEnum implements IBaseEnum { RED, YELLOW, BLUE; @Override public IBaseEnum[] getList() { return values(); } @Override public int getIndex() { return ordinal(); } public ColorEnum getNext(){ return (ColorEnum) new Utils().next(this,false); } public ColorEnum getPrevious(){ return (ColorEnum) new Utils().previous(this,false); } } 

你也可以在界面中添加getNext / getPrevious