其实我的java程序就像......
public class Schedule {
public static enum RepeatType {
DAILY, WEEKLY, MONTHLY;
}
public static enum WeekdayType {
MONDAY(Calendar.MONDAY), TUESDAY(Calendar.TUESDAY),
WEDNESDAY(Calendar.WEDNESDAY), THURSDAY(Calendar.THURSDAY),
FRIDAY(Calendar.FRIDAY), SATURDAY(Calendar.SATURDAY), SUNDAY(Calendar.SUNDAY);
private int day;
private WeekdayType(int day) {
this.day = day;
}
public static List<Date> generateSchedule(RepeatType repeatType,List<WeekdayType> repeatDays) {
// ...
// here some logic i wrote
}
}
}
我将这个方法调用到我的Business类中,如下所示......
@RemotingInclude
public void createEvent(TimetableVO timetableVO) {
if ("repeatDays".equals(timetableVO.getSearchKey())) {
List<Date> repeatDaysList = Schedule.generateSchedule(timetableVO.getRepeatType() , timetableVO.getRepeatDays());
}
}
最后TimetableVO
是
@Entity
@Table(name="EC_TIMETABLE")
public class TimetableVO extends AbstractVO {
// ...
private RepeatType repeatType;
// But in this case the method generateSchedule(-,-) was not calling.
private List<WeekdayType> repeatDays;
// ...
}
所以我的问题是以下哪一个是更好的声明...
private List<WeekdayType> repeatDays;
(或)
// If we give like this `How to Convert Enum type to String` because generateSchedule() method taking enum type value....
private String repeatDays;
答案 0 :(得分:1)
每个枚举类型都内置了方法name()
,以使枚举名称为String
,valueOf(String)
朝向另一个方向。这可能就是你要找的东西。
答案 1 :(得分:1)
可以使用toString()方法将任何枚举类型的值转换为String。您可能没有意识到(我之前没有)如果您声明RepeatType
变量r1
,那么可以使用{{r1
将String
转换为toString()
1}}方法EG r1.toString()
本计划:
public class Main {
/** Creates a new instance of Main */
public Main() {
}
public static enum RepeatType {
DAILY, WEEKLY, MONTHLY;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
RepeatType r1;
r1 = RepeatType.DAILY;
System.out.printf("R1 VALUE: %s\n", r1.toString());
r1 = RepeatType.WEEKLY;
System.out.printf("R1 VALUE: %s\n", r1.toString());
r1 = RepeatType.MONTHLY;
System.out.printf("R1 VALUE: %s\n", r1.toString());
}
}
将输出实际的枚举值如下:
R1 VALUE: DAILY
R1 VALUE: WEEKLY
R1 VALUE: MONTHLY
答案 2 :(得分:0)
最后我得到了正确答案,请使用以下内容..
@Entity
@Table(name="EC_TIMETABLE")
public class TimetableVO extends AbstractVO {
// ...
private RepeatType repeatType;
// But in this case the method generateSchedule(-,-) was not calling.
private WeekdayType repeatDays;
// ...
}
在我的服务方法中,我将Enum(WeekdayType)转换为List,因此很容易......
List<WeekdayType> weekdayList= new ArrayList<WeekdayType>();
weekdayList.add(timetableVO.getRepeatDays());
List<Date> repeatDaysList = Schedule.generateSchedule(timetableVO.getRepeatType() , weekdayList);