列挙定義定数(数値-文字列)


列挙クラス

package com.mark.java;

public enum Color {
	 (1, " "),  (2, " "),  (3, " "),  (4, " "),  (5, " ");
	private final int type;
	private final String color;

	private Color(int type, String color) {
		this.type = type;
		this.color = color;
	}

	public int getType() {
		return type;
	}

	public String getColor() {
		return color;
	}

	public static String getColorName(int type) {
		for (Color c : Color.values()) {
			if (type == c.getType()) {
				return c.getColor();
			}
		}
		return "";
	}
}

テストクラス

package com.mark.java;

public class ColorTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println(Color.getColorName(1));
		System.out.println(Color.getColorName(2));
		System.out.println(Color.getColorName(3));
		System.out.println(Color.getColorName(4));
		System.out.println(Color.getColorName(5));
	}

}

とても简単で、とても実用的です!!!