JAVA枚举类

一、方式一(无参数)

public enum Week {
	MON,TUE,WED;
}

相当于

public class Week {
	
	public static final Week MON = new Week();
	public static final Week TUE = new Week();
	public static final Week WED = new Week();
	
	private Week(){}						//私有构造,不让其他类创建本类对象
}
public static void demo1() {
   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;
	}
	
	public String toString() {
		return name;
	}
}

相当于

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;
	}

}
public static void demo2() {
   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();
	
}

相当于

public abstract class Week3 {
	
	public static final Week3 MON = new Week3("星期一") {
		public void show() {
			System.out.println("星期一");
		}
	};
	public static final Week3 TUE = new Week3("星期二"){
		public void show() {
			System.out.println("星期二");
		}
	};
	public static final Week3 WED = new Week3("星期三"){
		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();
}
public static void demo3() {
   Week3 mon = Week3.MON;
   mon.show();
}
public static void main(String[] args) {
   Week3 mon = Week3.TUE;
   switch (mon) {
   case MON:
      System.out.println("星期一");
      break;
   case TUE:
      System.out.println("星期二");
      break;
   }
}

四、使用方法

 int ordinal()
          返回枚举常量的序数(它在枚举声明中的位置,其中初始常量序数为零)。
 int compareTo(E o)
          比较此枚举与指定对象的顺序。
 String name()
          返回此枚举常量的名称,在其枚举声明中对其进行声明。
 String toString()
          返回枚举常量的名称,它包含在声明中。
static>T valueOf(Class enumType, String name)
          返回带指定名称的指定枚举类型的枚举常量。

values() 此方法虽然在JDK文档中查找不到,但每个枚举类都具有该方法,它遍历枚举类的所有枚举值非常方便

public static void demo1() {
   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());
   
   System.out.println(mon.compareTo(tue));          //比较的是编号
   System.out.println(mon.compareTo(wed));
   
   System.out.println(mon.name());             //获取实例名称
   System.out.println(mon.toString());          //调用重写之后的toString方法

   Week2[] arr = Week2.values();
   for (Week2 week2 : arr) {
      System.out.println(week2);
   }

}

 

 

你可能感兴趣的:(JAVA基础,JAVA,JAVA基础,枚举类)