Java教程例题3.2

来源:互联网 发布:淘宝网买家地域解读 编辑:程序博客网 时间:2024/06/07 01:57

1.用来体验下封装性

2.代码:

public class MyDate//类声明 {//成员变量private int year,month,day;private static int thisYear;static {thisYear=2009;}/*---------------------------设置对象属性的方法----------------------------------------------*/void set(int year,int month,int day)//成员方法,设置日期值{this.year=year;this.month=(month>=1&&month<=12)?month:1;this.day=(day>=1&&day<=31)?day:1;//这其实不一定能保证日期合法,比如二月的情况}void set(MyDate d){set(d.year,d.month,d.day);}/*------------------------------------------------------------------------------------------*//*----------------------------构造方法-------------------------------------------------------*/public MyDate(int year,int month,int day){this.set(year,month,day);}public MyDate(){this(1970,1,1);}public MyDate(MyDate d){this.set(d);}/*-------------------------------------------------------------------------------------------*//*-------------------------------获得对象属性的方法-------------------------------------------*/public int getYear(){return this.year;//}public int getMonth(){return this.month;}public int getDay(){return this.day;}public static int getThisYear(){return thisYear;}/*-------------------------------------------------------------------------------------------*//*-----------------------------------把类转化为字符串-----------------------------------------*/public String toString(){return year+"年"+month+"月"+day+"日";}/*-------------------------------------------------------------------------------------------*//*-------------------------------------判断闰年----------------------------------------------*/public static boolean isLeapYear(int year){return year%400==0||year%4==0&&year%100!=0;}public boolean isLeapYear(){return isLeapYear(this.year);//调用静态方法}/*-------------------------------------------------------------------------------------------*//*------------------------------------判断日期是否相等--------------------------------------------*/public boolean equals(MyDate d){return this==d||d!=null&&d.year==year&&d.month==month&&d.day==day;//this指代调用本方法的当前对象}/*-------------------------------------------------------------------------------------------*//*--------------------------------返回当月的天数-----------------------------------------*/public static int daysOfMonth(int year,int month){switch(month)//{case 1:return 31;case 3:return 31;case 5:return 31;case 7:return 31;case 8:return 31;case 10:return 31;case 12: return 31;case 4:case 6:case 9:case 11:return 30;case 2:return isLeapYear(year)?29:28;default: return 0;}}public int daysOfMonth(){return daysOfMonth(this.year,this.month);}/*---------------------------------改为第二天的日期----------------------------------------------*/public void tomorrow(){this.day++;if(day>this.daysOfMonth()){day=1;month++;if(month>12){month=1;year++;}}}/*-------------------------------------------------------------------------------------------*//*----------------------------------返回前一天的日期--------------------------------------------*/public MyDate yestoday(){MyDate yes=new MyDate(this);yes.day--;if(yes.day==0){yes.month--;if(yes.month==0){yes.year--;yes.month=12;}yes.day=daysOfMonth(yes.year,yes.month);}return yes;}/*-------------------------------------------------------------------------------------------*//*public static void main(String args[]){System.out.println("今年是"+MyDate.getThisYear()+", 闰年? "+MyDate.isLeapYear(MyDate.getThisYear()));MyDate d1=new MyDate(2008,8,8);MyDate d2=new MyDate(d1);System.out.println(d2+", 闰年? "+d2.isLeapYear());System.out.print(d2+" 的昨天是 "+d2.yestoday()+"\n"+d2+" 的明天是 ");d2.tomorrow();System.out.println(d2);}*/}class MyDate_ex{public static void main(String args[]){System.out.println("今年是"+MyDate.getThisYear()+", 闰年? "+MyDate.isLeapYear(MyDate.getThisYear()));MyDate d1=new MyDate(2008,8,8);MyDate d2=new MyDate(d1);System.out.println(d2+", 闰年? "+d2.isLeapYear());System.out.print(d2+" 的昨天是 "+d2.yestoday()+"\n"+d2+" 的明天是 ");d2.tomorrow();System.out.println(d2);}}


0 0
原创粉丝点击