日期:2014-05-20 浏览次数:20904 次
class Day { int d; public Day() { d = 1; } public Day(int i) { if (i >= 1 && i <= 7) d = i; else return; } public void setDate(int i) { d = i; } public Day getDate() { return new Day(d); } public void nextDate() { addSomeDate(1); } public void beforeDate() { addSomeDate(-1); } public void addSomeDate(int i) { d = (d + i) % 7; } public String toString() { String week = ""; switch (d) { case 1: week = "Mon"; break; case 2: week = "Tue"; break; case 3: week = "Wen"; break; case 4: week = "Thu"; break; case 5: week = "Fri"; break; case 6: week = "Sat"; break; default: week = "Sun"; break; } return week; } } public class TestDay { public static void main(String[] args) { // a,设置日期 Day day = new Day(); day = new Day(2); // b,输出日期 // c, 返回日期的名称 System.out.println(day.getDate()); // d, 返回下一天的名称 day.nextDate(); System.out.println(day.getDate()); // e, 返回前一天的名称 day.beforeDate(); System.out.println(day.getDate()); // f, 给当前日期加上一些天数,并返回。 day.addSomeDate(3); System.out.println(day.getDate()); } }