标签:
An enum type is a special data type that enables for a variable to be a set of predefined constants. The constructor of enum must be private. It means the same thing as making it package private. The only way to instantiate an enum is by declaring them within your enum class. Enums cannot have public constructors.
package Enum; public class FoodEnumDemo { public enum Food { HAMBURGER(7), FRIES(2), HOTDOG(3), ARTICHOKE(4); Food(int price) { this.price = price; } private final int price; public int getPrice() { return price; } } public static void main(String[] args) { for (Food f : Food.values()) { System.out.print("Food: " + f + ", "); if (f.getPrice() >= 4) { System.out.print("Expensive, "); } else { System.out.print("Affordable, "); } switch (f) { case HAMBURGER: System.out.println("Tasty"); break; case ARTICHOKE: System.out.println("Delicious"); break; default: System.out.println("OK"); } } } }
标签:
原文地址:http://www.cnblogs.com/touchdown/p/5169964.html