java枚举


package newer.demo;

public class Demo {

//定义枚举类实现星期。
public static final int monday=1;
public static final int tuesday=2;
public static final int wednesday=3;
public static final int thursday=4;
public static final int friday=5;
public static final int saturday=6;
public static final int sunday=7;

private static Demo s1=new Demo("星期一");
private static Demo s2=new Demo("星期二");
private static Demo s3=new Demo("星期三");
private static Demo s4=new Demo("星期四");
private static Demo s5=new Demo("星期五");
private static Demo s6=new Demo("星期六");
private static Demo s7=new Demo("星期七");
private String info;
public Demo(String info) {
// TODO Auto-generated constructor stub
this.info=info;
}
public void display(){
System.out.println(this.info);
}
public static Demo getInstance(int i){
switch(i){
case 1:return s1;
case 2:return s2;
case 3:return s3;   
case 4:return s4;
case 5:return s5;
case 6:return s6;
default :return s7;
}
}
}

======================================================
//定义枚举

package newer.demo;

public enum DemoEnum {
monday("星期一"),
        tuesday("星期二"),
        wednesday("星期三"),
       thursday("星期四"),
       friday("星期五"),
       saturday("星期六"),
       sunday("星期七");
private String info;

private DemoEnum(String info) {
this.info = info;
}

public void display() {
System.out.println(this.info);
}

}

=============================================================
//测试
package newer.demo;

public class Text {
public static void main(String[] args) {
DemoEnum.friday.display();
DemoEnum.thursday.display();
}
}

你可能感兴趣的:(java)