package 枚举; //向枚举添加自定义属性 enum Apple { AURORA(10),BELMAC(12),CORTLAND(15),EMPIRE(8),GRAVENSTEIN(11); private int price; //构造方法 Apple(int price) { this.price = price; } int getPrice() { return price; } } public class ApplesEnum { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("苹果价格列表"); for(Apple apple:Apple.values()) { System.out.println(apple+"花费 "+apple.getPrice()+"分钱."); } } }
苹果价格列表 AURORA花费 10分钱. BELMAC花费 12分钱. CORTLAND花费 15分钱. EMPIRE花费 8分钱. GRAVENSTEIN花费 11分钱.
Java编程:向枚举添加自定义属性(用for-each循环来打印每种苹果的名称和价格)
原文地址:http://blog.csdn.net/u012110719/article/details/41017721