spe*_*dRS 5 java enums object instantiation
首先,如果这是现有问题的副本,请道歉.不确定如何说出我的问题,这可能就是为什么我还没有找到一个明确的答案.基本上,我想知道以下是否被视为良好做法或是否有更好的方法来做到这一点:
public enum ExampleEnum {
ENTRY_1(new ExampleCodedValue("entry1", "comment1")),
ENTRY_2(new ExampleCodedValue("entry2", "comment2")),
ENTRY_3(new ExampleCodedValue("entry3", "comment3")),
ENTRY_4(new ExampleCodedValue("entry4", "comment4"));
private ExampleCodedValue codedValue;
ExampleEnum(ExampleCodedValue codedValue) {
this.codedValue = codedValue;
}
public ExampleCodedValue getCodedValue() {
return codedValue;
}
}
class ExampleCodedValue {
private final String code;
private final String comment;
ExampleCodedValue(String code, String comment) {
this.code = code;
this.comment = comment;
}
}
Run Code Online (Sandbox Code Playgroud)
Jef*_*rey 11
这是一个非常合理的方法,但是,你不能这样做:
public enum ExampleEnum {
ENTRY_1("entry1", "comment1");
private final String entry;
private final String comment;
private ExampleEnum(String entry, String comment) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9354 次 |
| 最近记录: |