如何在自定义Set类中格式化ZodiacType关联的ZodiacInfo名称?
解决ZodiacType枚举输出完整星座名称的问题
问题场景
我开发的Java程序中存在带构造方法的ZodiacType枚举,每个常量关联ZodiacInfo枚举与日期区间。自定义Set类用于存储ZodiacType实例,当前toString方法及getStringZodiacInfoFormat方法输出的是ZodiacType的常量名(如GEM),但期望输出对应的ZodiacInfo完整名称(如Gemini),现有代码无法实现该转换。
现有代码
ZodiacType枚举
public enum ZodiacType { ARI(ZodiacInfo.Aries, "March 21", "April 19"), TAU(ZodiacInfo.Taurus, "April 20", "May 20"), GEM(ZodiacInfo.Gemini, "May 21", "June 20"), CAN(ZodiacInfo.Cancer, "June 21", "July 22"), LEO(ZodiacInfo.Leo, "July 23", "August 22"), VIR(ZodiacInfo.Virgo, "August 23", "September 22"), LIB(ZodiacInfo.Libra, "September 23", "October 22"), SCO(ZodiacInfo.Scorpio, "October 23", "November 21"), SAG(ZodiacInfo.Sagittarius, "November 22", "December 21"), CAP(ZodiacInfo.Capricorn, "December 22", "January 19"), AQU(ZodiacInfo.Aquarius, "January 20", "February 18"), PIS(ZodiacInfo.Pisces, "February 19", "March 20"); private final ZodiacInfo description; private final String startDate; private final String toDate; ZodiacType(ZodiacInfo description, String startDate, String toDate) { this.description = description; this.startDate = startDate; this.toDate = toDate; } }
自定义Set类
public class Set { private ArrayList<ZodiacType> s; public Set() { s = new ArrayList<>(); } @Override public String toString() { return String.format("A = {%s}", s.toString().replace("[", "").replace("]", "")); } public String getStringZodiacInfoFormat() { ArrayList<String> strings = new ArrayList<>(); for (ZodiacType zodiacType: s) { strings.add(zodiacType.name()); } return String.format("A = {%s}", strings.toString().replace("[", "").replace("]", "")); } }
当前输出
Notation in enum format A = {GEM, LIB, CAN, PIS, SCO, ARI, VIR} ......................................................
期望输出
Notation in enum format A = {Gemini, Libra, Cancer, Pisces, Scorpio, Aries, Virgo} ......................................................
解决方案
核心思路
要实现转换,关键是获取ZodiacType中关联的ZodiacInfo实例并输出其名称,需要完成两步调整:
- 给
ZodiacType添加ZodiacInfo的访问方法(getter),因为原代码中description是私有字段,外部无法直接访问; - 修改输出方法,替换原有的
zodiacType.name()调用,改为获取ZodiacInfo的名称;或重写ZodiacType的toString方法,让其默认输出完整名称。
修改后的代码示例
1. 调整ZodiacType枚举
添加getter方法,并可选重写toString简化集合输出:
public enum ZodiacType { ARI(ZodiacInfo.Aries, "March 21", "April 19"), TAU(ZodiacInfo.Taurus, "April 20", "May 20"), GEM(ZodiacInfo.Gemini, "May 21", "June 20"), CAN(ZodiacInfo.Cancer, "June 21", "July 22"), LEO(ZodiacInfo.Leo, "July 23", "August 22"), VIR(ZodiacInfo.Virgo, "August 23", "September 22"), LIB(ZodiacInfo.Libra, "September 23", "October 22"), SCO(ZodiacInfo.Scorpio, "October 23", "November 21"), SAG(ZodiacInfo.Sagittarius, "November 22", "December 21"), CAP(ZodiacInfo.Capricorn, "December 22", "January 19"), AQU(ZodiacInfo.Aquarius, "January 20", "February 18"), PIS(ZodiacInfo.Pisces, "February 19", "March 20"); private final ZodiacInfo description; private final String startDate; private final String toDate; ZodiacType(ZodiacInfo description, String startDate, String toDate) { this.description = description; this.startDate = startDate; this.toDate = toDate; } // 提供获取关联ZodiacInfo的方法 public ZodiacInfo getDescription() { return description; } // 可选:重写toString,让枚举默认输出完整星座名称 @Override public String toString() { return description.name(); } }
2. 修改自定义Set类的输出方法
public class Set { private ArrayList<ZodiacType> s; public Set() { s = new ArrayList<>(); } @Override public String toString() { // 若ZodiacType已重写toString,直接使用集合默认toString转换 return String.format("A = {%s}", s.toString().replace("[", "").replace("]", "")); } public String getStringZodiacInfoFormat() { ArrayList<String> strings = new ArrayList<>(); for (ZodiacType zodiacType : s) { // 替换为获取ZodiacInfo的完整名称 strings.add(zodiacType.getDescription().name()); } return String.format("A = {%s}", strings.toString().replace("[", "").replace("]", "")); } }
效果验证
修改后,两种输出方法都会返回期望的完整星座名称格式,与目标输出一致。
内容的提问来源于stack exchange,提问作者Ming Liang
相关产品推荐
相关产品推荐

