package com.wsy.test; public enum Color {
RED("红色",1),GREEN("绿色",2),BLUE("蓝色",3);
private String name;
private int index;
private Color(String name , int index) {
this.name = name;
this.index = index;
}
@Override
public String toString()
{
return "颜色为:"+this.name+" 序号为:"+this.index;
}
}
package com.wsy.test; public enum SexEnum {
MALE,FAMALE; }
package com.wsy.test; public class Test { public static void main(String[] args) {
Color c1 = Color.RED;
switch(c1)
{
case RED:
c1 = Color.BLUE;
break;
case BLUE:
c1 = Color.GREEN;
break;
case GREEN:
c1 = Color.RED;
break;
default:
c1 = null;
break;
}
System.out.println(c1);
} }
package com.wsy.test; public class Test2 { public static void main(String[] args) {
// for(int i =0; i<Color.values().length; i++)
// {
// System.out.println(Color.values()[i]);
// }
for(Color c:Color.values())
{
System.out.println(c);
} } }
package com.wsy.test; public class Test3 { public static void main(String[] args) {
Color c1 = Color.BLUE;
for(int i =0; i<Color.values().length; i++)
{
System.out.println(c1+"与"+Color.values()[i]+"的比较结果是"+c1.compareTo(Color.values()[i]));
}
//显示的结果证明如果相等的话返回0 如果想了解更多可以进入源代码查看
for(int i=0; i<Color.values().length; i++)
{
System.out.println("索引:"+Color.values()[i].ordinal()+" 值:"+Color.values()[i]);
}
//索引和数组下标一样 从0开始
} }
package com.wsy.test; public class Test4 {
public static void main(String[] args) {
for(WeekDay day:WeekDay.values())
{
System.out.println(day+"=====>"+day.getDay());
}
WeekDay.printDay(1);
}
}
package com.wsy.test; public enum WeekDay {
MON("Monday"),TUE("Tuesday"),WED("Wednesday"),THU("Thursday"),FRI("Friday"),SAT("Saturday"),SUN("Sunday");
private String day;
private WeekDay(String day)
{
this.day = day;
}
public static void printDay(int day)
{
switch(day)
{
case 1:
System.out.println(WeekDay.MON);
break;
case 2:
System.out.println(WeekDay.TUE);
break;
case 3:
System.out.println(WeekDay.WED);
break;
case 4:
System.out.println(WeekDay.THU);
break;
case 5:
System.out.println(WeekDay.FRI);
break;
case 6:
System.out.println(WeekDay.SAT);
break;
case 7:
System.out.println(WeekDay.SUN);
break;
default:
System.out.println("error");
break;
}
}
public String getDay() {
return day;
} }