代码如下
分别是enum和testclass两个java文件
package cs121assignment1;
public enum Food {
APPLE("fruit", 55),
BANANA("fruit", 80),
CARROT("vegetable", 60);
private final String catagory; //vegetable or fruit
private final int calorie;
Food(String catagory, int calorie){
this.catagory = catagory;
this.calorie = calorie;
}
public int getCalorie(){
return calorie;
}
public String getCatagory(){
return catagory;
}
}
package cs121assignment1;
public class TestFood {
public static void main(String[] args){
System.out.println("All foods:");
for(Food food : Food.values()){
System.out.printf("%s, catagory: %s, calorie: %d kilocalorie each
", food, food.getCatagory(), food.getCalorie());
}
}
eclipse中运行结果如下:
但是用命令行执行javac的时候显示如下:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…