java枚举
jdk5.0之前的
public class SessonTest {
public static void main(String[] args) {
Seasson spring = Seasson.Spring;
System.out.println(spring);
}
}
class Seasson{
//1.
private final String seassonName;
private final String seassonDesc;
//2.
private Seasson(String seassonName, String seassonDesc){
this.seassonName = seassonName;
this.seassonDesc = seassonDesc;
}
//3.提供枚举类的多个对象
public static final Seasson Spring = new Seasson("春天","美丽");
//4.
public String getSeassonName() {
return seassonName;
}
public String getSeassonDesc() {
return seassonDesc;
}
//4.
@Override
public String toString() {
return "Seasson{" +
"seassonName='" + seassonName + '\'' +
", seassonDesc='" + seassonDesc + '\'' +
'}';
}
}
jdk5.0之后
import java.lang.reflect.Array;
import java.util.Arrays;
/*
enum的方法
1.values:所有的枚举类元素
2.valueOf:根据提供的objName返回同名的枚举类元素,如果没有的话抛异常
3.toString:
1.实现接口
2.
*/
public class enumTestDemo {
public static void main(String[] args) {
Seasson1 spring = Seasson1.Spring;
System.out.println(spring);//Spring
Seasson1[] values = Seasson1.values();
System.out.println(Arrays.toString(values));//[Spring, Summer]
for(int i = 0;i < values.length;i++){
values[i].show();
}
Thread.State[] values1 = Thread.State.values();
System.out.println(Arrays.toString(values1));//[NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED]
Seasson1 spring1 = Seasson1.valueOf("Spring");
System.out.println(spring1);//Spring
spring1.show();
Seasson1 summer = Seasson1.valueOf("Summer");
summer.show();
}
}
interface Info{
void show();
}
enum Seasson1 implements Info{
Spring("春天","美丽"){
@Override
public void show() {
System.out.println("春天美丽");
}
},
Summer("夏天","清爽"){
@Override
public void show() {
System.out.println("夏天清爽");
}
};
//1.
private final String seassonName;
private final String seassonDesc;
//2.
private Seasson1(String seassonName, String seassonDesc){
this.seassonName = seassonName;
this.seassonDesc = seassonDesc;
}
//3.提供枚举类的多个对象
//4.
public String getSeassonName() {
return seassonName;
}
public String getSeassonDesc() {
return seassonDesc;
}
//4.
// @Override
// public String toString() {
// return "Seasson1{" +
// "seassonName='" + seassonName + '\'' +
// ", seassonDesc='" + seassonDesc + '\'' +
// '}';
// }
// @Override
// public void show() {
// System.out.println("这是一个季节");
// }
}