一、枚举类型
(一)枚举类型是类的一种特殊形式,用于声明一组命名的常量。
enum Student{//枚举名
ZHANGSAN,//枚举体
LISI,
WANGWU;
}
(二)声明枚举就是定义一种特殊形式的类,但是不能通过关键字new创建它的实例对象。枚举类型的枚举值 ZHANGSAN, LISI, WANGWU就是Student类的实例对象。
Student student = Student.WANGWU;//枚举成员默认都是public static final的(常量)。所以枚举类的枚举值全部大写
注:
(1)在枚举声明当中只允许public,不允许abstract和final修饰,枚举不能是抽象的,也不允许派生;
(2)enum关键字(小写);
(3)任意两个枚举体不能有相同的名字。
(三)访问枚举的方式与静态常量字段相似
package practise;
enum Color{//定义枚举
RED,GREEN,WHITE,BLACK
}
public class enum_test {
public static void main(String[] args) {
Color color = Color.BLACK;
Color color1 = Color.GREEN;
System.out.println(color.ordinal());//返回枚举序号
System.out.println(color.toString());//返回枚举对象
System.out.println(color.name());//枚举名
System.out.println(Color.valueOf("RED"));
System.out.println("====================");
//?
System.out.println(color.equals(color1));
System.out.println(color == color1);
System.out.println(color.compareTo(color1));//比较序号差值a.compareTo(b) a的序号减去b的序号
System.out.println(color1.compareTo(color));
System.out.println("===================");
System.out.println(color.getDeclaringClass());//枚举类的名字及路径
System.out.println("============");
//.values() 把枚举的实例都变成一个数组,
// 这个方法是由编译器自己生成的静态方法(源码中没有)
for (Color c:Color.values()) {
System.out.println(c);
}
System.out.println("==============");
Color[] colors = Color.values();
for(int i = 0;i < colors.length;i ++){
System.out.println(colors[i]);
}
Color color2 = Color.RED;
switch (color){
case RED:
System.out.println("red");
break;
case GREEN:
System.out.println("green");
break;
case WHITE:
System.out.println("white");
break;
case BLACK:System.out.println("black");
break;
}
//Color color3 = new Color();ERROR 枚举不能new
}
}
二、声明枚举类型的其它成员
包括构造方法、方法和数据成员。
枚举类的构造方法与一般类的构造方法不同,枚举类的构造方法只能是private(可以省略,默认为private),构造方法只是在构造枚举值的时候被调用。
package com.en;
enum Color_2{
RED(255,0,0),GREEN(0,255,0),BIUE(0,0,255);
private int redValue;
private int greenValue;
private int blueValue;
private Color_2(int rv,int gv,int bv){
this.redValue = rv;
this.greenValue = gv;
this.blueValue = bv;
}
public String toString(){
return super.toString() + "(" + redValue + "," + greenValue + "," + blueValue + ")";
}
}
public class TestEnum_2 {
public static void main(String[] args) {
Color_2 c = Color_2.BIUE;
System.out.println(c);//隐式调用toString方法
System.out.println(c.toString());//调用toString方法
}
}
三、枚举类的成员方法
所有的枚举类都隐式地继承于Enum类,所以所有的枚举类都继承了Enum类的方法。声明枚举类型时,编译器会自动提供一些方法。
package com.en;
enum Student{
ZHANGSAN,
LISI,
WANGWU;
}
public class TestEnum {
public static void main(String[] args) {
Student s = Student.WANGWU;//枚举成员默认都是public static final的(常量)。所以枚举类的枚举值全部大写
Student s1 = Student.ZHANGSAN;
System.out.println(s.ordinal());//2 返回枚举常量的序号
System.out.println(s.compareTo(s1));//2 s-s1
System.out.println(s1.compareTo(s));//-2 s1-s 比较两个枚举对象的序号
System.out.println("===================");
System.out.println(s.name());//WANGWU 返回该枚举常量的名字 (隐式调用toString方法)
System.out.println(s1.toString());//ZHANGSAN 返回该枚举常量的名字
System.out.println("===================");
Student s2 = Student.valueOf("LISI");//返回指定名称的指定枚举类型的枚举常量
Student[] ss = Student.values();
for(int i = 0;i < ss.length;i ++){
System.out.println(ss[i]);
}
System.out.println("===================");
for (Student s_1:Student.values()) {
System.out.println(s_1);
}
}
}