JDK1.5以前
package com.xiaozhi.enumration;
public class Test2 {
}
//class Grade{
// private Grade(){}
// public static final Grade A=new Grade();
// public static final Grade B=new Grade();
// public static final Grade C=new Grade();
// public static final Grade D=new Grade();
//}
enum Grade{
A,B,C,D;
}
带抽象方法的枚举
package com.xiaozhi.enumration;
public class Test {
public static void main(String[] args) {
for(Grade a:Grade.values())
System.out.println(a.getScore());
}
}
enum Grade{
A("90-100"){
public String getScore(){
return "优";
}
},
B("80-90"){
public String getScore(){
return "良";
}
},
C("70-80"){
public String getScore(){
return "中";
}
},
D("60-70"){
public String getScore(){
return "差";
}
};
private String value;
private Grade(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public abstract String getScore();
}
valueOf()方法的使用
package com.xiaozhi.enumration;
public class Test2 {
public static void main(String[] args) {
System.out.println(Grade.valueOf("A").getScore());
}
}
enum Grade{
A("90-100"){
public String getScore(){
return "优";
}
},
B("80-90"){
public String getScore(){
return "良";
}
},
C("70-80"){
public String getScore(){
return "中";
}
},
D("60-70"){
public String getScore(){
return "差";
}
};
private String value;
private Grade(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public abstract String getScore();
}
ordinal()返回枚举做坐标