什么是枚举?
枚举:可以理解为一个特殊的类,存放固定的常量,减少条件判断,枚举是值类型,枚举也是单例的一种(一个枚举类只存放一个对象)。
如何定义枚举:
enum TestEnum01 implements TestInterface {
Monday("周一"){
public String getValue(){
return "好天气";
}
@Override
public String getSex() {
// TODO Auto-generated method stub
return str;
}
},Tuesday("周二"){
public String getValue(){
return "不好天气";
}
@Override
public String getSex() {
// TODO Auto-generated method stub
return str;
}
},Wednesday("周三"){
public String getValue(){
return "天气";
}
@Override
public String getSex() {
// TODO Auto-generated method stub
return str;
}
},Thursday("周四"){
public String getValue(){
return "好天";
}
@Override
public String getSex() {
// TODO Auto-generated method stub
return str;
}
}, Friday("周五"){
public String getValue(){
return "好气";
}
@Override
public String getSex() {
// TODO Auto-generated method stub
return str;
}
};
private String name;
//枚举的构造函数
private TestEnum01(String value){
this.name = value;
}
public String getName(){
return name;
}
//抽象方法
public abstract String getValue();
public String str = "xixi哈哈";
}
使用枚举
public static void main(String[] args) {
System.out.println("枚举的构造函数"+TestEnum01.Friday.getValue());
System.out.println("枚举的抽象方法"+TestEnum01.Friday.getName());
System.out.println("枚举的接口"+TestEnum01.Monday.getSex());
System.out.println("枚举的数组"+TestEnum01.values());
System.out.println("枚举的下标"+TestEnum01.Monday.ordinal());
}