枚举:把所有可用的情况全都列举出来
当一个类能够创建出来的实例对象个数是固定并且对象明确时,可以考虑使用枚举
实现步骤:
1.把这个类的构造方法私有化,精致外部创建实例对象
2.在类的内部创建好public static final修饰的实例对象,让外部能够直接访问
3.定义一个有参的构造方法,创建对象时调用有参的构造方法,传入对象名
4.重写toString()方法,返回对象名
public class Month {
private String name;
private Month(String name) {
this.name = name;
}
private Month() {
}
public static final Month JAN = new Month("JAN");
public static final Month FEB = new Month("FEB");
public static final Month MAR = new Month("MAR");
public static final Month APR = new Month("APR");
public static final Month MAY = new Month("MAY");
public static final Month JUN = new Month("JUN");
public static final Month JUL = new Month("JUL");
public static final Month AUG = new Month("AUG");
public static final Month SEP = new Month("SEP");
public static final Month OCT = new Month("OCT");
public static final Month NOV = new Month("NOV");
public static final Month DEC = new Month("DEC");
@Override
public String toString() {
return name;
}
}
public class Singleton {
private Singleton() {
}
private static Singleton instance = new Singleton();
public static Singleton getInstance() {
return instance;
}
}public class SingletonDemo {
public static void main(String[] args) {
Singleton x1 = Singleton.getInstance();
Singleton x2 = Singleton.getInstance();
Singleton x3 = Singleton.getInstance();
Singleton x4 = Singleton.getInstance();
System.out.println(x1 == x2); //true
System.out.println(x2 == x3); //true
System.out.println(x3 == x4); //true
// 手动的实现了枚举,把所有可以用的对象都在类的内部创建好
// 禁止外部创建实例对象,外部只能直接使用
System.out.println(Month.JAN); // JAN
System.out.println(Month.MAY); // MAY
System.out.println(Month.NOV); // NOV
}
}JDK1.5以后,提供了枚举类型可以快速的实现枚举功能
枚举的特点:
1.使用关键字enum来声明一个枚举类型
2.枚举里的第一行代码,必须要列出所有的实例对象
可以认为 所有的实例对象都是被public static final 修饰的
3.枚举里也可以定义构造方法,但是构造必须要被private修饰,默认构造方法就是被private修饰的
4.枚举里创建实例对象不需要使用关键字new,也不需要手动写构造方法
如果调用的是空参的构造方法,()也可以省略
public static final Day Mon = new Day(); ==> MON(); ==> MON;
5.所有的枚举类型都继承自Enum类
6.枚举不能再继承其他的类!Java里不支持多继承,一个类最多只能有一个父类
public enum Day {
/*
public static final Day MON = new Day();
*/
MON(), TUE, WED("周三"), THU, FRI, SAT, SUN("周日");
Day() {
}
private String x;
Day(String x) {
this.x = x;
}
@Override
public String toString() {
if (x != null) return x;
return super.toString();
}
}public class EnumDemo {
public static void main(String[] args) {
System.out.println(Day.MON);
System.out.println(Day.TUE);
System.out.println(Day.WED);
System.out.println(Day.THU);
System.out.println(Day.FRI);
System.out.println(Day.SAT);
System.out.println(Day.SUN);
/*
打印一个枚举对象,也是调用枚举对象的toString()方法
为什么调用枚举对象的toString()方法时,打印的结果不是 类型@哈希值
因为定义的枚举类型默认全都继承自 Enum类,在 Enum类里重写了toString()方法
*/
System.out.println(Season.SPRING);
System.out.println(Season.SUMMER.name()); // 获取到对象名
System.out.println(Season.AUTUMN.toString());
System.out.println(Season.WINTER.ordinal()); // 获取到对象的编号,从0开始
Season x = Season.valueOf("SPRING"); // 根据字符串获取到 SPRING枚举
System.out.println(Season.SPRING == x); // true
Season[] seasons = Season.values(); // 获取到所有的枚举对象
}
}常见方法:
| 方法名 | 作用 |
| toString | 返回的是常量名(对象名),可以重写 |
| name | 返回的是常量名(对象名),不推荐使用,建议使用toString |
| values | 返回该枚举类的所有的常量对象,返回类型是当前枚举的数组类型。 |
| valueOf | 根据常量名,返回一个枚举对象。 |
| ordinal | 返回枚举常量的序数(它在枚举声明中的位置,其中初始常量序数为零)。 |
版权声明:本文为weixin_44216665原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。