如何为一些Java枚举添加通用方法?(抽象类祖先?)
我有几个这样的Java枚举
public enum Aggregation{
MORTGAGE( "Mortgage" ),
POOLS( "Pools" ),
PORTFOLIO( "Portfolio" );
private Aggregation( final String name )
{
m_Name = name;
}
private String m_Name;
static Map< String, Aggregation > c_LOOKUP =
new HashMap< String, Aggregation >();
static {
for (Aggregation agg:values()){
c_LOOKUP.put(agg.m_Name,agg);
}
}
public Aggregation lookup(String name){
return c_LOOKUP.get( name );
}
@Override
public String toString()
{
return m_Name;
}
}
public enum Interval
{
MONTHLY( "Monthly" ),
QUARTLY( "Quartly" ),
SEMIANNUALLY( "SemiAnnually" ),
ANNUALLY("Annually");
private Interval( final String name )
{
m_Name = name;
}
private String m_Name;
static Map< String, Interval > c_LOOKUP =
new HashMap< String, Interval >();
static {
for (Interval agg:values()){
c_LOOKUP.put(agg.m_Name,agg);
}
}
public Interval lookup(String name){
return c_LOOKUP.get( name );
}
@Override
public String toString()
{
return m_Name;
}
}
如您所见,这里有很多代码重复。如果有一种方法可以引入诸如抽象的共同祖先类之类的东西,那就太好了。但是java枚举不能固有。最好的方法是什么?谢谢。
编辑:我已经制定出一个类似于ŁukaszBachman和missingfacktor的版本
static public enum Aggregation{
MORTGAGE( "Mortgage" ),
POOLS( "Pools" ),
PORTFOLIO( "Portfolio" );
private final String m_Name;
final static private ReverseDictionary< Aggregation > c_DICTIONARY =
new ReverseDictionary< Aggregation >( Aggregation.class );
static public Aggregation lookup( final String name )
{
return c_DICTIONARY.lookup( name );
}
private Aggregation( final String name )
{
m_Name = name;
}
@Override
public String toString()
{
return m_Name;
}
}
static public enum Interval
{
MONTHLY( "Monthly" ),
QUARTLY( "Quartly" ),
SEMIANNUALLY( "SemiAnnually" ),
ANNUALLY( "Annually" );
private final String m_Name;
final static private ReverseDictionary< Interval > c_DICTIONARY =
new ReverseDictionary< Interval >( Interval.class );
static public Interval lookup( final String name )
{
return c_DICTIONARY.lookup( name );
}
private Interval( final String name )
{
m_Name = name;
}
@Override
public String toString()
{
return m_Name;
}
}
static public class ReverseDictionary< E extends Enum< E >>
{
Map< String, E > c_LOOKUP = new HashMap< String, E >();
public ReverseDictionary( final Class< E > enumClass )
{
for( final E agg : EnumSet.allOf( enumClass ) )
{
c_LOOKUP.put( agg.toString(), agg );
}
}
public E lookup( final String name )
{
return c_LOOKUP.get( name );
}
}
我看到了一些推理。但是,它仍然不是很令人满意。
lookup(String)
由于返回类型不同,因此很难为其定义接口- 我可以理解,
lookup(String)
不是真正的重复,而是一个规范,但是我仍然感到m_Name字段和toString()逻辑有点多余。我们实际上是在指定一种枚举类型,在我看来,这似乎是 关系。
回答:
偏爱 和
。由于枚举是类(不是常规的,但仍然是类),因此您可以创建一些包含共享逻辑的字段,让枚举实现您的接口并将实现委派给该字段。
相关代码段:
public interface MyInterface { void someMethod();
}
public class MyInterfaceImpl implements MyInterface { public void someMethod() {
System.out.println("Do smth...");
}
}
public enum EnumA implements MyInterface { ;
private MyInterface impl = new MyInterfaceImpl();
public void someMethod() {
impl.someMethod();
}
}
public enum EnumB implements MyInterface { ;
private MyInterface impl = new MyInterfaceImpl();
public void someMethod() {
impl.someMethod();
}
}
请大家注意,EnumA
而EnumB
不是真正的重复代码,因为这是普通的委托(有效的,在我看来)。还请注意,使用接口可以将所有内容很好地粘合在一起。
以上是 如何为一些Java枚举添加通用方法?(抽象类祖先?) 的全部内容, 来源链接: utcz.com/qa/410102.html