java-枚举示例

发布时间:2016-12-11 21:57:03 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"java-枚举示例",主要涉及到java-枚举示例方面的内容,对于java-枚举示例感兴趣的同学可以参考一下。

package cd.itcast.day1; /** * 声明了一个枚举类型,这个类型里面只有Monday,Tuesday,Wednesday,Thursday,Friday,Saturday, * Sunday几个实例 * * @author Administrator * */ public enum WeekDays { /** * 下面每一个定义的枚举实例,实际上都经过了这样一段代码: public static WeekDays Monday=new * WeekDays("Monday",0); * * Monday("Monday",0); * * class Monday extends WeekDays{ public WeekDays next(){ * * } } * */ Monday("星期一") { public WeekDays next() { return Tuesday; } }, Tuesday("星期二") { public WeekDays next() { return Wednesday; } }, Wednesday("星期三") { public WeekDays next() { return Thursday; } }, Thursday("星期四") { public WeekDays next() { return Friday; } }, Friday("星期五") { public WeekDays next() { return Saturday; } }, Saturday("星期六") { public WeekDays next() { return Sunday; } }, Sunday("星期日"); /** * private WeekDays(){ super(name,ordinal); System.out.println("构造方法"); } */ // private WeekDays(){ // System.out.println("构造方法。。"); // } /** * 继承方法 */ public WeekDays next2() { /** * 可以使用枚举参与switch,但是实际上,比较的仍然是下标 */ switch (this) { case Monday: return WeekDays.Tuesday; case Tuesday: return WeekDays.Wednesday; case Wednesday: return WeekDays.Thursday; case Thursday: return WeekDays.Friday; case Friday: return WeekDays.Saturday; case Saturday: return WeekDays.Sunday; default: return WeekDays.Monday; } // switch (this.ordinal()) { // case 0: // return WeekDays.Tuesday; // case 1: // return WeekDays.Wednesday; // case 2: // return WeekDays.Thursday; // case 3: // return WeekDays.Friday; // case 4: // return WeekDays.Saturday; // case 5: // return WeekDays.Sunday; // default: // return WeekDays.Monday; // } } /** * 为枚举增加构造方法 */ private WeekDays(String chineseName) { this.chineseName = chineseName; } /** * 给枚举类型增加了属性 */ private String chineseName; public WeekDays next() { return Monday; } /** * 给枚举类型增加了方法 */ public String getChineseName() { return chineseName; } @Override public String toString() { return this.chineseName; } }

上一篇:java-注解annotation
下一篇:block 神秘的面纱

相关文章

关键词: java-枚举示例

相关评论