Java 枚举构造函数和方法


Java 枚举构造函数和方法

enum Car {
   lamborghini(900),tata(2),audi(50),fiat(15),honda(12);
   private int price;

   Car(int p) {
      price = p;
   }

   int getPrice() {
      return price;
   }
}

public class Main {
   public static void main(String args[]) {
      System.out.println("All car prices:");

      for (Car c : Car.values())
         System.out.println(c + " costs " + c.getPrice() + " thousand dollars.");
   }
}