java enum enumeration usage example

java enum enumeration usage example


public class TestEnum {

	public enum Color {
		GREEN(4), YELLOW(2), RED(1);
		
		// To use "()" for assignment, be sure to add the following lines
		// ------------------------------------
		private final int value;

		// The constructor can only be private by default, thus ensuring that the constructor can only be used internally
		Color(int value) {
			this.value = value;
		}

		public int getValue() {
			return value;
		}
		// ------------------------------------
	}

	public static void main(String[] args) {
		Color color = Color.GREEN;
		System.out.println("color Enum == " + color.getValue());
		System.out.println("color Enum array value == " + color.ordinal());
	}

}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326929083&siteId=291194637