Enum are Class Types

来源:互联网 发布:mac 浅色文件夹打不开 编辑:程序博客网 时间:2024/05/29 10:50
 * don't instantiate a enum using new *///Use an enum Constructor, instance variable, and method.enum Apple{   Jonathan(10), GoldenDel(9), RedDel(12), Winesap(15), Cortland(8);   private int price;   //contructor   Apple(int p) { price= p;}   //Overloaded constructor   Apple() { price= -1;}   int getPrice(){return price;}}class EnumDemo3{   public static void main(String args[]) {      Apple ap;      //Display price of Winesap.      System.out.println("Winesap costs "+ Apple.Winesap.getPrice()+ " cents.\n");      //Display all apples and prices.      System.out.println("All apple prices: ");      for(Apple a: Apple.values())         System.out.println(a+" costs"+ a.getPrice()+ " cents.");   }}
0 0
原创粉丝点击