下面有一个例子,
public class WeekDay1 {
private WeekDay1(){}
public final static WeekDay1 SUN = new WeekDay1();
public final static WeekDay1 MON = new WeekDay1();
public WeekDay1 nextDay(){
if(this == SUN){
return MON;
}else{
return SUN;
}
}
public String toString(){
return this == SUN?"SUN":"MON";
}
}
public class EnumTest {
public static void main(String[] args) {
WeekDay1 w = WeekDay1.SUN;
System.out.println(w.nextDay());
}
}
打印结果为:MON
调用的是nextDay方法,最后却打印出了toString()方法里的MON,nextDay与toString这个方法之间有什么关系,以及toString()这个方法是在什么时候被调用的?
有点迷糊,请高手指点一下! |