枚举类
一个类中的对象 认为个数是有限且固定的 可以将每一个对象一一列举出来;
JDK1.5版本之后可以直接定义enum类型;
我们自己定义的enum类直接默认继承Enum(java.lang包)
我们自己定义的enum类型不能再写extends 但是可以实现;
package eenum; /* public enum Day{ //描述了七个当前类的对象 monday("星期一",1),tuesday("星期二",1),wednesday,thursday,friday,saturday,sunday private String name; private int index private Day(){ } private Day(String name,int index){ this.name=name; this.index=index; } public String getName(){ return this.name; } public void setName(String name){ this.name=name; } } */ public enum Day{ //描述了七个当前类的对象 monday,tuesday,wednesday,thursday,friday,saturday,sunday } /* public enum Day{ //描述了七个当前类的对象 monday,tuesday,wednesday,thursday,friday,saturday,sunday } */ /* public class Day{//7个对象 private Day(){ }//构造方法私有 //所有对象都是属性 public static final Day monday=new Day(); public static final Day tuesday=new Day(); public static final Day wednesday=new Day(); public static final Day thursday=new Day(); public static final Day friday=new Day(); public static final Day saturday=new Day(); public static final Day sunday=new Day(); } */
package eenum; import java.util.Scanner; public class Test{ public static void main(String[] args){ Scanner input=new Scanner(System.in); System.out.println("请输入一个星期的英文单词"); String dd=input.nextLine(); Day day=Day.valueOf(dd); switch(day){ case monday: System.out.println("星期一"); break; case tuesday: System.out.println("星期二"); break; case wednesday: System.out.println("星期三"); break; case thursday: System.out.println("星期四"); break; case friday: System.out.println("星期五"); break; case saturday: System.out.println("星期六"); break; case sunday: System.out.println("星期日"); break; default: System.out.println("错误"); } } } /* public class Test{ public static void main(String[] args){ Day a=Day.monday; Day[] ds=Day.values(); for(Day d:ds){ System.out.println(d.name()+"--"+d.ordinal()); } System.out.println(); Day d=Day.valueOf("monday"); System.out.println(d.name()+"=="+d.ordinal()); } } */ /* public class Test{ public static void main(String[] args){ Day day=Day.monday; } } */
原文:https://www.cnblogs.com/canglongdao/p/12891556.html