java-enumeration

Enum

  • The enumeration class can implement one or more interfaces. The enumeration class defined by enum inherits the java.lang.Enum class by default instead of the Object class by default, so the enumeration class cannot display inheritance from other parent classes . The java.lang.Enum class implements two interfaces, java.lang.Serializable and java.lang.Comparable.
  • Enum-defined, non-abstract enumeration classes that use enum will default to final decoration , so enumeration classes cannot be derived from subclasses .
  • The constructor of the enumeration class can only use the private access control character . If the access control character of the constructor is omitted, the private modifier is used by default; if the access control character is mandatory, only the private modifier can be specified .
  • Enum class of all instances must be explicitly listed in the first line enumeration class , otherwise the enumeration class never not produce examples . When listing these instances, the system will automatically add the public static final decoration, without the need for programmers to explicitly add.
  • The enumeration class provides a values ​​() method by default, which can easily traverse all enumeration values
enum Week {

	//全是大写字母表示就是枚举的实例对象,枚举只能在自己首行代码实例化自己的对象
	MONDAY("jis"),TUESDAY("2"),WEDNESDAY("3"),THURSDAY("4"),FRIDAY("5"),SATURDAY("6"),SUNDAY("6");
//MONDAY : 就是一个具体的对象;   public static final Week MONDAY = new Week("");
	String i = "Tom";
	private Week(String a) {
		
		i = a;
	}
	
	public void test() {
		
		System.out.println( this.name()+"里面的test方法");
	}
}

import java.util.Arrays;

public class Test1 {

	public static void main(String[] args) {
		
		System.out.println(Week.MONDAY.i);
		System.out.println(Week.SATURDAY.equals(Week.FRIDAY));
		System.out.println(Week.WEDNESDAY.ordinal());
		System.out.println(Week.WEDNESDAY.name());
		
		Week[] week = Week.values();
		System.out.println(Arrays.toString(week));
		choose(Week.MONDAY);

	}
	
	public static void choose(Week w) {
		
		switch(w) {
		
		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;
		}
	}

}
Published 6 original articles · liked 0 · visits 85

Guess you like

Origin blog.csdn.net/ShaoWeiY/article/details/104546446