本帖最后由 nailsoul 于 2012-6-8 10:31 编辑
- package aa;
- import static java.lang.System.out;
- class EnumTest {
- static WeekdayDemo day;
- public static void main(String[] args) {
- day=WeekdayDemo.SUNDAY.nextDay();
- out.println(day+" "+day.pos);
- day.nextDay();
- out.println(day+" "+day.pos);
- out.println();
- day=WeekdayDemo.MONDAY;
- sop();
- day=WeekdayDemo.valueOf(1);
- sop();
-
- out.println();
-
-
- day=WeekdayDemo.MONDAY;
- sop1();
- day=WeekdayDemo.valueOf(1);
- sop1();
-
- out.println();
- day=WeekdayDemo.DAYS[0];
- out.println(day+" "+day.pos);
- out.println(WeekdayDemo.DAYS[WeekdayDemo.TUSDAY.pos]);
- }
- public static void sop(){
- for(int i=0;i<8;i++){
- out.print(day+" "+day.pos+"\t");
- day=day.nextDay();
- }
- out.println();
- }
- public static void sop1(){
- for(int i=0;i<8;i++){
- out.print(day+" "+day.pos+"\t");
- day=day.nextDay1();
- }
- out.println();
- }
- }
- class WeekdayDemo{
- private String name;
- public int pos;
- public static final WeekdayDemo MONDAY=new WeekdayDemo("MONDAY",1);
- public static final WeekdayDemo TUSDAY=new WeekdayDemo("TUSDAY",2);
- public static final WeekdayDemo WEDNESDAY=new WeekdayDemo("WEDNESDAY",3);
- public static final WeekdayDemo THURSDAY=new WeekdayDemo("THURSDAY",4);
- public static final WeekdayDemo FRIDAY=new WeekdayDemo("FRIDAY",5);
- public static final WeekdayDemo SATURDAY=new WeekdayDemo("SATURDAY",6);
- public static final WeekdayDemo SUNDAY=new WeekdayDemo("SUNDAY",0);
- public static final WeekdayDemo[] DAYS=new WeekdayDemo[] {SUNDAY,MONDAY,TUSDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY};
- private WeekdayDemo(){}
- private WeekdayDemo(String name,int pos){
- this.name=name;
- this.pos=pos;
- }
- public WeekdayDemo nextDay(){
- return DAYS[pos==6?0:++pos];
- }
- public WeekdayDemo nextDay1(){
- int index=0;
- for(;index<DAYS.length;index++){
- if(this==DAYS[index]){
- break;
- }
- }
- return DAYS[index==6?0:++index];
- }
-
- public static WeekdayDemo valueOf(int index){
- return(DAYS[index<7?index:0]);
- }
-
-
-
- @Override
- public String toString(){
- return this.name;
- }
-
- }
复制代码 结果为:
MONDAY 1
MONDAY 2
MONDAY 1 TUSDAY 2 WEDNESDAY 3 THURSDAY 4 FRIDAY 5 SATURDAY 6 SUNDAY 0 MONDAY 2
MONDAY 3 THURSDAY 5 SATURDAY 6 SUNDAY 1 TUSDAY 3 THURSDAY 6 SUNDAY 2 WEDNESDAY 4
MONDAY 4 TUSDAY 4 WEDNESDAY 5 THURSDAY 6 FRIDAY 6 SATURDAY 6 SUNDAY 3 MONDAY 4
MONDAY 4 TUSDAY 4 WEDNESDAY 5 THURSDAY 6 FRIDAY 6 SATURDAY 6 SUNDAY 3 MONDAY 4
SUNDAY 4
WEDNESDAY
结果为什么是这样子的哦 ? pos是对象的成员变量记录是的角标 怎么pos跟name不对号呢? 第一行正确的 怎么地2行是MONDAY 2 而不是MONDAY 1啊?
最后2到4行名字正确了 角标为什么还是不正确啊 最后一行为什么取出来的是WEDNESDAY哦?
郁闷 昨天想了半天都不知道哪里错了 今天早上吃早餐时 稍微想下就想通了 看来 当想不通一个问题时 还是等段时间在想才是正解啊。。。。
public WeekdayDemo nextDay(){
//return DAYS[pos==6?0:++pos]; ++pos把对象的pos+1 导致了 一个对象调用一次这个方法它的pos值就+1 在根据pos值取对象自然就取错了
return DAYS[pos==6?0:1+pos];
}
day=WeekdayDemo.SUNDAY.nextDay(); //SUNDAY的pos值变成了1 day=MONDAY
out.println(day+" "+day.pos); //打印Monday 1
day.nextDay(); //Monday的pos值自增变成了2 day.nextDay();的返回值THUSDAY没赋给day所以day=MONDAY
out.println(day+" "+day.pos); //打印MONDAY 2
out.println(); //打印"\n"
day=WeekdayDemo.MONDAY; //day=Monday
//i=0 打印前day=MONDAY //打印MONDAY 2 //打印后MONDAY的pos值为3 day=WEDNESDAY;
//i=1 day=WEDNESDAY; //打印WEDNESDAY 3 //WEDNESDAY的pos值为4 day=THURSDAY;
//....................
//i=4 day=SATURDAY //打印SATURDAY 6 //SATURDAY.pos=6 day=SUNDAY
//i=5 day=SUNDAY //打印 SUNDAY 1 //SUNDAY.POS=2 day=TUSDAY
//i=6 day=TUSDAY //打印TUSDAY 2 //TUSDAY.pos=3 day=WEDNESDAY
//i=7 day=WEDNESDAY //打印WEDNESDAY 4 //WEDNESDAY.pos=5 day=THURSDAY
sop();
//所以到这里为止打印的是
// MONDAY 1
// MONDAY 2
// MONDAY 2 WEDNESDAY 3 THURSDAY 4 FRIDAY 5 SATURDAY 6 SUNDAY 1 TUSDAY 2 WEDNESDAY 4
day=WeekdayDemo.valueOf(1);
//i=0 打印前day=MONDAY //打印MONDAY 3 //打印后MONDAY的pos值为4 day=THURSDAY;
//i=1 day=THURSDAY; //打印THURSDAY 5 //THURSDAY的pos值为6 day=SATURDAY
//i=2 day=SATURDAY //打印SATURDAY 6 //SATURDAY.pos=6 day=SUNDAY
//i=3 day=SUNDAY //打印SUNDAY 2 //SUNDAY.POS=3 day=WEDNESDAY
//i=4 day=WEDNESDAY //打印WEDNESDAY 5 //WEDNESDAY.pos=6 day=SATURDAY
//i=5 day=SATURDAY //打印 SATURDAY 6 //SATURDAY.POS=6 day=SUNDAY
//i=6 day=SUNDAY //打印SUNDAY 3 //SUNDAY.pos=4 day=THURSDAY
//i=7 day=THURSDAY //打印THURSDAY 6 //SATURDAY.pos=6 day=SUNDAY
sop();
//上面一句打印MONDAY 3 THURSDAY 5 SATURDAY 6 SUNDAY 2 WEDNESDAY 5 SATURDAY 6 SUNDAY 3 THURSDAY 6
out.println();
day=WeekdayDemo.MONDAY;
sop1();
//打印的MONDAY到SUNDAY的pos值经过上面的语句一句不是初值了
//打印 MONDAY 4 TUSDAY 3 WEDNESDAY 6 THURSDAY 6 FRIDAY 6 SATURDAY 6 SUNDAY 4 MONDAY 4
day=WeekdayDemo.valueOf(1);
sop1();
//打印 MONDAY 4 TUSDAY 3 WEDNESDAY 6 THURSDAY 6 FRIDAY 6 SATURDAY 6 SUNDAY 4 MONDAY 4
out.println();
day=WeekdayDemo.DAYS[0];
out.println(day+" "+day.pos);
//打印SUNDAY 4 因为SUNDAY的pos为4
out.println(WeekdayDemo.DAYS[WeekdayDemo.TUSDAY.pos]);
//打印WEDNESDAY 因为TUSDAY的pos为3 DAYS[3]为WEDNESDAY
/*
所以最终结果为
MONDAY 1
MONDAY 2
MONDAY 2 WEDNESDAY 3 THURSDAY 4 FRIDAY 5 SATURDAY 6 SUNDAY 1 TUSDAY 2 WEDNESDAY 4
MONDAY 3 THURSDAY 5 SATURDAY 6 SUNDAY 2 WEDNESDAY 5 SATURDAY 6 SUNDAY 3 THURSDAY 6
MONDAY 4 TUSDAY 3 WEDNESDAY 6 THURSDAY 6 FRIDAY 6 SATURDAY 6 SUNDAY 4 MONDAY 4
MONDAY 4 TUSDAY 3 WEDNESDAY 6 THURSDAY 6 FRIDAY 6 SATURDAY 6 SUNDAY 4 MONDAY 4
SUNDAY 4
WEDNESDAY
*/
|
|