- /*作业要求:用枚举编写一个星期
- 枚举值:MON TUE WED THU FRI SAT SUN
- 该枚举要有一个方法返回中文的星期几。*/
- public class work {
- public static void main(String[] args)
- {
- printDay(week.Monday);
- }
- public static void printDay(week day)
- {
- System.out.println(day.getDay());
- }
- }
- /*//方法一:利用构造函数复制
- enum week
- {
- Monday("星期一"),Tuesday("星期二"),Wednesday("星期三"),Thursday("星期四"),Friday("星期五"),Saturday("星期六"),sunday("星期日");
- private String day;
- private week(String day)
- {
- this.day = day;
- }
- public String getDay()
- {
- return day;
- }
- }*/
- //方法二:利用抽象方法调用
- enum week
- {
- Monday
- {
- public String getDay()
- {
- return "星期一";
- }
- },
- Tuesday
- {
- public String getDay()
- {
- return "星期二";
- }
- },
- Wednesday
- {
- public String getDay()
- {
- return "星期三";
- }
- },
- Thursday
- {
- public String getDay()
- {
- return "星期四";
- }
- },
- Friday
- {
- public String getDay()
- {
- return "星期五";
- }
- },
- Saturday
- {
- public String getDay()
- {
- return "星期六";
- }
- },
- Sunday
- {
- public String getDay()
- {
- return "星期日";
- }
- };
- private week() {};
- public abstract String getDay();
- }
复制代码 能看懂 你就了解的差不多了,枚举特点就是当只有一个对象时 他实际就是单例 |