- public class WeekDay {
- private WeekDay(){}
- public final static WeekDay SUN=new WeekDay();
- public final static WeekDay MON=new WeekDay();
- public WeekDay nextDay()
- {
- if(this==SUN)
- {
- return MON;
- }
- else
- {
- return SUN;
- }
- }
- public String toString()
- {
- return this==SUN?"SUN":"MON";
- }
- }
复制代码- public class EnumTest {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- WeekDay weekDay=WeekDay.MON;
- System.out.println(weekDay.nextDay());
- }
- }
复制代码
这时答案就是SUN
我调用的是nextDay()
他为什么自动调用toString方法呢?
|
|