* 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.");
}
}
Enum are Class Types
最新推荐文章于 2024-02-24 14:43:49 发布