测试类
/**
* @param args
*/
public static void main(String[] args) {
//demo1();
//demo2();
Week3 mon = Week3.MON;
mon.show();
}
public static void demo1() {
Week mon = Week.MON;
Week tue = Week.TUE;
Week wed = Week.WED;
System.out.println(mon);
}
public class Week {
public static final Week MON = new Week();
public static final Week TUE = new Week();
public static final Week WED = new Week();
private Week(){} //私有构造,不让其他类创建本类对象
}
public static void demo2() {
Week2 mon = Week2.MON;
System.out.println(mon.getName());
}
public class Week2 {
public static final Week2 MON = new Week2("星期一");
public static final Week2 TUE = new Week2("星期二");
public static final Week2 WED = new Week2("星期三");
private String name;
private Week2(String name){
this.name = name;
} //私有构造,不让其他类创建本类对象
public String getName() {
return name;
}
}
public abstract class Week3 {
public static final Week3 MON = new Week3("星期一") {
//匿名类
public void show() {
System.out.println("星期一");
}
};
public static final Week3 TUE = new Week3("星期二"){
public void show() {
System.out.println("星期二");
}
};
public static final Week3 WED = new Week3("星期三"){
public void show() {
System.out.println("星期三");
}
};
private String name;
private Week3(String name){
this.name = name;
} //私有构造,不让其他类创建本类对象
public String getName() {
return name;
}
public abstract void show();
}
原文:http://blog.51cto.com/357712148/2158323