问题描述
只需播放,并提出了一种可以在,其中包含。
进一步的修补使我能够几乎也增加了一个整洁(即不抛出异常)反向查找,但有一个问题。报告:
错误:X中的valueOf(String)无法在HasValue中实现valueOf(String)
public enum X实现PoliteEnum,ReverseLookup {
覆盖方法是静态
有没有办法? >
这里的目的是默认添加(通过一个接口实现与默认
方法,如我添加的在链接的答案中的politeName
)查找
方法,执行 valueOf
函数而不抛出例外。可能吗?现在很明显,现在可以扩展枚举
,这是Java的主要问题之一,直到现在为止。
这是我的失败尝试:
public interface HasName {
/ pre>
public String name();
}
public interface PoliteEnum extends HasName {
default String politeName(){
return name()。replace(_, );
}
}
public interface Lookup< P,Q> {
public Q lookup(P p);
}
public interface HasValue {
HasValue valueOf(String name);
}
public interface ReverseLookup扩展HasValue,Lookup< String,HasValue> {
@Override
默认值HasValue lookup(String from){
try {
return valueOf(from);
} catch(IllegalArgumentException e){
return null;
}
}
}
public枚举X实现PoliteEnum / * NOT ALLOWED :(,ReverseLookup * / {
A_For_Ism,B_For_Mutton,C_Forth_Highlanders;
}
public void test(){
//测试(X x:X.values())的politeName
{
System.out.println(x.politeName());
}
// ToDo:测试查找
}
解决方案您的设计过于复杂,如果您愿意接受您可以调用
默认
方法,整个代码可能如下所示:interface ReverseLookupSupport< ; E扩展Enum< E> {
Class< E> getDeclaringClass();
默认E lookup(String name){
try {
return Enum.valueOf(getDeclaringClass (),name);
} catch(IllegalArgumentException ex){return null;}
}
}
枚举测试实现ts ReverseLookupSupport< Test> {
FOO,BAR
}
您可以使用以下测试: p>
Test foo = Test.FOO;
测试条= foo.lookup(BAR),baz = foo.lookup(BAZ);
System.out.println(bar ++ baz);
一个不投掷/捕捉的替代方案是:
interface ReverseLookupSupport< E extends Enum& E> {
Class< E> getDeclaringClass();
default可选< E> lookup(String name){
return Stream.of(getDeclaringClass()。getEnumConstants())
.filter(e-> e.name()。equals(name))findFirst()
}
使用喜欢:
Test foo = Test.FOO;
测试条= foo.lookup(BAR)orElse(null),baz = foo.lookup(BAZ)orElse(null);
System.out.println(bar ++ baz);
Just playing and came up with a sweet way to add functionality to
enum
s in Java Enum toString() method with this.Some further tinkering allowed me to nearly also add a tidy (i.e. not throwing an exception) reverse look-up but there's a problem. It's reporting:
error: valueOf(String) in X cannot implement valueOf(String) in HasValue public enum X implements PoliteEnum, ReverseLookup { overriding method is static
Is there a way?
The aim here is to silently add (via an interface implementation with a
default
method like I addedpoliteName
in the linked answer) alookup
method that does thevalueOf
function without throwing an exception. Is it possible? It is clearly now possible to extendenum
- one of my major problems with Java until now.Here's my failed attempt:
public interface HasName { public String name(); } public interface PoliteEnum extends HasName { default String politeName() { return name().replace("_", " "); } } public interface Lookup<P, Q> { public Q lookup(P p); } public interface HasValue { HasValue valueOf(String name); } public interface ReverseLookup extends HasValue, Lookup<String, HasValue> { @Override default HasValue lookup(String from) { try { return valueOf(from); } catch (IllegalArgumentException e) { return null; } } } public enum X implements PoliteEnum/* NOT ALLOWED :( , ReverseLookup*/ { A_For_Ism, B_For_Mutton, C_Forth_Highlanders; } public void test() { // Test the politeName for (X x : X.values()) { System.out.println(x.politeName()); } // ToDo: Test lookup }
解决方案You are over-complicating your design. If you are willing to accept that you can invoke a
default
method on an instance only, there entire code may look like this:interface ReverseLookupSupport<E extends Enum<E>> { Class<E> getDeclaringClass(); default E lookup(String name) { try { return Enum.valueOf(getDeclaringClass(), name); } catch(IllegalArgumentException ex) { return null; } } } enum Test implements ReverseLookupSupport<Test> { FOO, BAR }
You can test it with:
Test foo=Test.FOO; Test bar=foo.lookup("BAR"), baz=foo.lookup("BAZ"); System.out.println(bar+" "+baz);
An non-throwing/catching alternative would be:
interface ReverseLookupSupport<E extends Enum<E>> { Class<E> getDeclaringClass(); default Optional<E> lookup(String name) { return Stream.of(getDeclaringClass().getEnumConstants()) .filter(e->e.name().equals(name)).findFirst(); }
to use like:
Test foo=Test.FOO; Test bar=foo.lookup("BAR").orElse(null), baz=foo.lookup("BAZ").orElse(null); System.out.println(bar+" "+baz);
这篇关于是否可以在Java 8中扩展枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!