目前我在几个类上定义了几个枚举。它们看起来都类似于下面所示的:
public class ApaMessage {
private String apaMessage;
private final int FIXED_LENGTH_SIZE=39;
public enum ApaFields {
FIELD1(ApaUtils.ApaFieldTypes.POSITION_BASED, null, "field1", 2, 3, false, false),
private final ApaUtils.ApaFieldTypes type;
private final String ApaName;
private final String jsonName;
private final int start;
private final int finish;
private boolean required = false;
private boolean withDelimiter = false;
ApaFields(ApaUtils.ApaFieldTypes type, String ApaName, String jsonName, int start, int finish, boolean required, boolean withDelimiter) {
this.type = type;
this.ApaName = ApaName;
this.jsonName = jsonName;
this.start = start;
this.finish = finish;
this.required = required;
this.withDelimiter = withDelimiter;
}
}
ApaMessage中还定义了一个方法:
private HashMap<String,Object> getApaJsonFieldsAndValues() {
HashMap<String, Object> jsonApaData = new HashMap<String, Object>();
for (ApaFields field : ApaFields.values()) {
jsonApaData.put(field.jsonName, getApaFieldValue(field));
}
return jsonApaData;
}
问题是虽然代码不多,但我很快就会有 10-20 个这样的枚举。我想创建一个抽象基类,其中 HashMap 方法和其他类似方法可以是其中的一部分。基类应该接受 ApaFields 枚举和其他枚举,并执行 getApaJsonFieldsAndValues 的操作。问题是,基类如何访问传递的枚举值和内部字段(例如 jsonName)来执行循环?
我尝试了不同的方法,但主要问题是基类似乎无法访问这些值。有没有办法解决?或者,有更好的方法吗?谢谢
编辑:
基本上我希望在基类中有这样的东西。请注意,以下内容无法编译。
公共(public)抽象类ApaRequestMessage {
private Class<? extends Enum<?>> apaRequestMessageFields;
private String apaMessage;
public <T extends Enum<T>> void ApaRequest(Object apaRequestFields, String apaMessage) {
apaRequestMessageFields = (Class<? extends Enum<?>>) apaRequestFields;
this.apaMessage = apaMessage;
for (Field field: apaRequestMessageFields.values()) {
//this doesn't work because it cannot access the values of apaRequestMessageFields
}
}
}
然后按如下方式调用基本方法,尽管不确定这是否正确,其中 ApaFields 是上面定义的内部枚举。
ApaRequest(ApaFields.class, somestringmessage);
最佳答案
在尝试使用枚举作为表中的列来定义数据库模式时,我遇到了类似的情况。我最终选择了这条路。
定义一个具有足够通用签名的基类,以确保正确构建枚举。
public class Table<Column extends Enum<? extends Column>> {
// Name of the table.
protected final String tableName;
// All of the columns in the table. This is actually an EnumSet so very efficient.
protected final Set<Column> columns;
/**
* The base interface for all Column enums.
*/
public interface Columns {
// What type does it have in the database?
public Type getType();
}
// Small list of database types.
public enum Type {
String, Number, Date;
}
public Table(String tableName,
Set<Column> columns) {
this.tableName = tableName;
this.columns = columns;
}
}
现在为每个表扩展它 - 这是一个简单的VersionTable
:
public class VersionTable extends Table<VersionTable.Column> {
public enum Column implements Table.Columns {
Version(Table.Type.String),
ReleaseDate(Table.Type.Date);
final Table.Type type;
Column(Table.Type type) {
this.type = type;
}
@Override
public Type getType() {
return type;
}
}
public VersionTable() {
super("Versions", EnumSet.allOf(Column.class));
}
}
现在您已经拥有基类中的所有核心功能,所有子类需要做的就是在枚举
上实现接口(interface)。
我意识到这并不能解决所有枚举中重复的 bolierplate 代码的问题,但它确实将所有代码移到了其他地方。
关于java - 创建一个抽象基类来处理不同类型的枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36398055/