Using Enum type in Java 在Java中使用泛型

Categories: Java; Tagged with: ; @ July 16th, 2012 21:05

Enum is a new feature shipped in Java 5, An enum type is a type whose fields consist of a fixed set of constants.

/**
 * Specify the currency type.
 * @author Guoliang
 */
public static enum  Currency{
	/** NICKLE */
	N("NICKLE", 0.05),
	/** DIME */
	D("DIME", 0.10),
	/** QUARTER */
	Q("QUARTER", 0.25),
	/** DOLLAR */
	DOLLAR("DOLLAR", 1.00);

	/** Abbr of the money type. */
	private String dispalyName;
	/** the value of the money. */
	private double value;

	// Constructor
	private Currency(String abbr, double value) {
		this.dispalyName = abbr;
		this.value = value;
	}

	public String getAbbr() {
		return dispalyName;
	}

	public double getValue() {
		return value;
	}

	public static void main(String[] args) {
		for (Currency currency : Currency.values()) {
			System.out.println(currency.getAbbr());
		}
	}

}

Java.lang.Enum is an abstract Class, so Enum can not extends other classes, btw, should not create enum instance, so the constructor need to be private: “only private is permitted.”

Ref: http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

 

 

<->



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.