abstract class Enum{
public Enum () {
System.out.println("Enum");
}
public static final Enum Sat = new Enum(){
@Override
public Enum nextDay() {
// TODO Auto-generated method stub
return null;
}
};
public static final Enum Sun = new Enum(){
@Override
public Enum nextDay() {
// TODO Auto-generated method stub
return null;
}
};
public abstract Enum nextDay();
// public Enum nextDay() {
// if(this == Sat){
// return Sun;
// }else{
// return Sat;
// }
//
// }
public String toString() {
return (this == Sat ? "Sat":"Sun");
}
}
/*
* 分析出来的结果:
* ①由此对比我们看出 枚举变量实质应该是个类的实例
* ②枚举变量在使用前,就已经初始化完毕 -->参考模拟Enum
* */
enum Weekday{
FRI{
@Override
public Weekday nextLamp() {
// TODO Auto-generated method stub
return null;
}
},SAT(2){
@Override
public Weekday nextLamp() {
// TODO Auto-generated method stub
return null;
}
};
private int time;
private Weekday(){
System.out.println("First");
}
private Weekday(int time){
System.out.println("Second");
this.time = time;
}
public abstract Weekday nextLamp();
}
public class MyEnum {
public static void main(String[] args) {
// TODO Auto-generated method stub
Enum satEnum = Enum.Sat;
// System.out.println(satEnum);
Weekday weekday1 = Weekday.SAT;
// Weekday weekday2 = Weekday.SAT;
// Weekday weekday3 = Weekday.WED;
}
}
Java 枚举变量解析
最新推荐文章于 2023-05-23 08:48:31 发布