首页 > 学院 > 开发设计 > 正文

Enum are Class Types

2019-11-08 20:00:17
字体:
来源:转载
供稿:网友
* 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."); }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表