public enum Test001 {
EXCEL("C:/TEST.xls", "哈.xls"),
WORD("C:/TEST.DOC", "哦.doc"),
;
private Test001(String templatePath, String fileName) { // 参数分别对应 C:/TEST.xls,哈.xls
System.out.println("init"); // 有多少个枚举就执行多少次构造方法
this.templatePath = templatePath;
this.fileName = fileName;
}
private final String templatePath;
private final String fileName;
public String getTemplatePath() {
return templatePath;
}
public String getFileName() {
return fileName;
}
private static Map<String, Test001> map = new HashMap<String, Test001>();
static {
for (Test001 in : Test001.values()) { // 把Test001中所有枚举都存放到 map中
map.put(in.name(), in);
}
};
public static Test001 get(String name) {
return map.get(name);
}
public static void main(String[] args) {
Test001 t = Test001.get("EXCEL");
String name = t.getFileName();
String path = t.getTemplatePath();
System.out.println(name);
System.out.println(path);
}
EXCEL("C:/TEST.xls", "哈.xls"),
WORD("C:/TEST.DOC", "哦.doc"),
;
private Test001(String templatePath, String fileName) { // 参数分别对应 C:/TEST.xls,哈.xls
System.out.println("init"); // 有多少个枚举就执行多少次构造方法
this.templatePath = templatePath;
this.fileName = fileName;
}
private final String templatePath;
private final String fileName;
public String getTemplatePath() {
return templatePath;
}
public String getFileName() {
return fileName;
}
private static Map<String, Test001> map = new HashMap<String, Test001>();
static {
for (Test001 in : Test001.values()) { // 把Test001中所有枚举都存放到 map中
map.put(in.name(), in);
}
};
public static Test001 get(String name) {
return map.get(name);
}
public static void main(String[] args) {
Test001 t = Test001.get("EXCEL");
String name = t.getFileName();
String path = t.getTemplatePath();
System.out.println(name);
System.out.println(path);
}
本文深入探讨了Java中枚举类的应用,以Test001为例,详细介绍了如何使用枚举类来管理配置文件路径和文件名,并通过实例展示了枚举类的构造方法、属性获取和静态方法实现的功能。
1697

被折叠的 条评论
为什么被折叠?



