您不能具有另一个
enum扩展
enum,也
enum不能通过继承将值“添加”到现有值。
但是,
enum可以实现
interface。
我要做的是让原始
enum实现标记
interface(即没有方法声明),然后您的客户端可以创建自己的
enum实现
interface。
然后,您的
enum价值观将被他们的共同点所引用
interface。
为了加强要求,您可以让介面宣告相关的方法,例如在您的情况下,在的行中
public String getHTTPMethodType();。
这将强制实现
enums提供该方法的实现。
此设置加上适当的API文档应有助于以相对受控的方式添加功能。
独立的示例 (不要介意这里的懒惰名称)
package test;import java.util.ArrayList;import java.util.List;public class Main { public static void main(String[] args) { List<HTTPMethodConvertible> 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; } }}输出量
firstPOST



