0

其实我的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
        }
    }
}

我正在将该方法调用到我的业务类中,如下所示......

@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; 
4

3 回答 3

1

每个枚举类型都有内置的方法name()来获取枚举名称作为 aStringvalueOf(String)朝另一个方向发展。这可能就是你要找的。

于 2012-12-14T05:05:16.397 回答
1

任何枚举类型的值都可以使用 toString() 方法转换为字符串。您可能没有意识到(我一开始没有意识到)如果您声明一个RepeatType变量r1,那么r1可以String使用toString()方法 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
于 2012-12-14T05:19:00.483 回答
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);
于 2012-12-14T09:34:09.417 回答