枚举概述:是指将变量的值一一列出来,变量的值只限于列举出来的值的范围内。 举例:一周只有7天,一年只有12个月等。 回想单例设计模式:单例类是一个类只有一个实例
那么多例类就是一个类有多个实例,但不是无限个数的实例,而是有限个数的实例。这才能是枚举类。 格式一:(无参构造)
public class Week1 { public static final Week1 MON =new Week1(); public static final Week1 TUE =new Week1(); public static final Week1 WED =new Week1(); //public static final Week MON =new Week(); private Week1() {} //私有构造,不让其他类创建本类对象 }测试:
Week1 mon =Week1.MON; Week1 tue =Week1.TUE; Week1 wed =Week1.WED; System.out.println(mon);格式二:(有参构造)
public class Week2 { public static final Week2 MON =new Week2("星期一"); public static final Week2 TUE =new Week2("星期二"); public static final Week2 WED =new Week2("星期三"); private String name; private Week2(String name) { this.name =name; } //私有构造,不让其他类创建本类对象 public String getName() { return name; }测试:
Week2 tue=Week2.TUE; System.out.println(tue.getName()); //输出结果为星期二格式三:(定义抽象方法) 因为是抽象类,所以不能生成一个对象,所以我们可以创建它子类,但是子类也不能调用父类的构造函数, 所以我们使用匿名内部类,花括号内相当于继承这个类,父类引用指向子类对象,子类重写父类的方法
public abstract class Week3 { //因为是抽象类,所以不能生成一个对象,所以我们可以创建它子类,但是子类也不能调用父类的构造函数, //所以我们使用匿名内部类,花括号内相当于继承这个类,父类引用指向子类对象,子类重写父类的方法 public static final Week3 MON =new Week3("星期一") { @Override public void show() { System.out.println("星期一"); } }; public static final Week3 TUE =new Week3("星期二") { @Override public void show() { System.out.println("星期二"); } }; public static final Week3 WED =new Week3("星期三") { @Override public void show() { System.out.println("星期三"); } }; private String name; private Week3(String name) { this.name =name; } //私有构造,不让其他类创建本类对象 public String getName() { return name; } public abstract void show(); }测试:
Week3 mon =Week3.MON; mon.show(); //输出结果为今天是星期一通过enum实现枚举类 方式一:(无参构造)
public enum Week { MON,TUE,WEN; }测试:
Week mon =Week.MON; System.out.println(mon);方式二:(有参构造)
public enum Week2 { MON("星期一"),TUE("星期二"),WED("星期三"); private String name; private Week2(String name) { this.name =name; } public String getName() { return name; } }测试:
Week2 mon =Week2.MON; System.out.println(mon.getName());方式三:(定义抽象方法)
public enum Week3 { MON("星期一"){ public void show() { System.out.println("星期一"); } },TUE("星期二"){ public void show() { System.out.println("星期二"); } },WED("星期三"){ public void show() { System.out.println("星期三"); } }; private String name; private Week3(String name) { this.name =name; } public String getName() { return name; } public abstract void show(); }测试:
Week3 mon =Week3.MON; mon.show();int ordinal()方法 返回枚举常量的序数(它在枚举声明中的位置,其中初始常量序数位0)
Week2 mon =Week2.MON; Week2 tue =Week2.TUE; Week2 wed =Week2.WED; //枚举都是有编号的 System.out.println(mon.ordinal()); System.out.println(tue.ordinal()); System.out.println(wed.ordinal());输出效果: int compareTo(E o)方法 比较枚举常量的位置
Week2 mon =Week2.MON; Week2 tue =Week2.TUE; Week2 wed =Week2.WED; System.out.println(mon.compareTo(tue)); //比较的是编号 System.out.println(mon.compareTo(wed));效果如下: String name()方法 输出枚举常量的名字
Week2 mon =Week2.MON; System.out.println(mon.name());效果如下: String toString()方法 第一步:书写枚举的toString方法
public enum Week2 { MON("星期一"),TUE("星期二"),WED("星期三"); private String name; private Week2(String name) { this.name =name; } public String getName() { return name; } public String toString() { return name; } } Week2 mon =Week2.MON; System.out.println(mon.toString());效果如下:
<泛型> T valueOf(Class<泛型> type,String name)
通过字节码文件获取枚举项对象,传入一个名称。可以更换,如果传入TUE就返回星期二
//通过字节码文件获取枚举项对象,传入一个名称。可以更换,如果传入TUE就返回星期二 Week2 mon =Week2.valueOf(Week2.class,"MON"); Week2 tue =Week2.TUE; Week2 wed =Week2.WED; System.out.println(mon);效果如下: values()
Week2[]arr =Week2.values(); for (Week2 week2 : arr) { //也可以通过week2获取getName方法 System.out.println(week2); }