java之 ------ 類的封裝、繼承和多態(一)

來源:互聯網
上載者:User

標籤:

問題:① 完善set()方法,保證獲得正確的日期值(合法性);

② 將tomorrow()和yestoday()方法合并為以下daysAfter()方法,並增加一些方法:

public int getWeek()   //返回當前日期對應的星期幾,範圍為0~6

public String toWeekString()   //返回當前日期對應星期幾的中文字串

public boolean before(MyDate d)  //判斷是否在指定日期之前

public int daysBetween(MyDate d) //返回當前日期與日期d之間相距的天數

public boolean equals(Object obj)  //比較兩個對象是否相等


import java.util.Calendar;import java.util.Scanner;public class MyDate2 {     int year,month,day;        boolean isCorrect=true;    private static int thisYear;      static {       thisYear=Calendar.getInstance().get(Calendar.YEAR); //獲得當前日期對象中的年份值    }    public MyDate2(int year, int month, int day) {        this.set(year, month, day);      }     public MyDate2()  {        this(1970,1,1);                          //調用本類已聲明的其他構造方法    }     public MyDate2(MyDate2 d)  {       this.set(d);    }    public void set(int y, int m, int d) {    if(y<1){    System.out.println("Input Year Is Error !!!");    this.isCorrect=false;    return ;    }if(m<1||m>12){    System.out.println("Input Month Is Error !!!");    this.isCorrect=false;    return ;    }    if(day<=0 || day>daysOfMonth() ){    System.out.println("Input Day Is Error !!!");    this.isCorrect=false;    return ;    }        this.year = y;                                 this.month = m;        this.day = d ;    }     public void set(MyDate2 d) {        set(d.year, d.month, d.day);             //調用同名成員方法,不能使用this()    }     public String toString(){        return this.year+"年"+this.month+"月"+this.day+"日";    }    public static int getThisYear(){        return thisYear;                         //訪問靜態成員變數    }    public static boolean isLeapYear(int year)  {        return year%400==0 || year%100!=0 && year%4==0;    }    public boolean isLeapYear() {        return isLeapYear(this.year);            //調用靜態方法    }            public boolean equals(MyDate2 d)  {                                            //this指代調用本方法的當前對象        return this==d || d!=null && this.year==d.year && this.month==d.month && this.day==d.day;    }     public boolean equals(Object obj)  {        if (this==obj)                           //this指代調用當前方法的對象            return true;        if (obj instanceof MyDate2) {            MyDate2 d = (MyDate2)obj;              //類型強制轉換            return this.year==d.year && this.month==d.month && this.day==d.day;        }        return false;    }        public static int daysOfMonth(int year, int month) {        switch (month)                           //計算每月的天數        {            case 1: case 3: case 5: case 7: case 8: case 10: 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++;                              //通過this改變當前對象的值,沒有傳回值        if (day>this.daysOfMonth()){            day=1;            month++;            if (month>12)            {                month=1;                year++;            }        }    }    public MyDate2 yestoday()     {        MyDate2 yes=new MyDate2(this);             //執行拷貝構造方法,建立新執行個體,沒有改變當前對象this        yes.day--;        if (yes.day==0) {            yes.month--;            if (yes.month==0){                yes.month=12;                yes.year--;            }            yes.day = daysOfMonth(yes.year, yes.month);        }        return yes;                              //返回對象引用    }    public MyDate2 dayAfter(int n)     {        MyDate2 yes=new MyDate2(this);             //執行拷貝構造方法,建立新執行個體,沒有改變當前對象this        yes.day+=n;        if (yes.day==0) {            yes.month--;            if (yes.month==0){                yes.month=12;                yes.year--;            }            yes.day = daysOfMonth(yes.year, yes.month);        //System.out.println("**"+yes.year+" "+yes.month+" "+yes.day);        return yes;        }         if (yes.day>this.daysOfMonth()){            yes.day=1;            yes.month++;            if (yes.month>12)            {                yes.month=1;                yes.year++;            }        }        return yes;                              //返回對象引用    }    public int getSumDays(MyDate2 d){    int  sum=0;    for(int i=1;i<d.year;i++){    sum+=d.isLeapYear(i) ? 366 : 365;    }    for(int i=1;i<d.month;i++){    sum+=d.daysOfMonth(d.year,i);    }    sum+=d.day;    return sum;    }    public int getWeek(){    long sum=0;    int week;    for(int i=1;i<this.year;i++){    sum+=this.isLeapYear(i) ? 366 : 365;    }    for(int i=1;i<this.month;i++){    sum+=this.daysOfMonth(this.year,i);    }    sum+=this.day;    week=(int)sum%7;    return week;    }    public String toWeekString(){    switch (getWeek()){    case 0 : return "Sunday" ;    case 1 : return "Monday" ;    case 2 : return "Tuesay" ;    case 3 : return "Wednesday" ;    case 4 : return "Thursday" ;    case 5 : return "Friday" ;    default : return "Saturday" ;    }    }    public boolean before(MyDate2 d){    if(this.year<d.year){    return true;    }else if(this.year==d.year){    if(this.month<d.month){    return true;    }else if(this.month==d.month){    if(this.day<d.day){    return true;    }else{    return false;    }    }else{    return false;    }    }else{    return false;    }    }    public int daysBetween(MyDate2 d){    int a=getSumDays(d);    int b=getSumDays(this);    return Math.abs(a-b);    }}class MyDate_ex{public static void main(String args[])    {    Scanner sc=new Scanner(System.in);   System.out.println("Please input date : ");    while(sc.hasNext()){        MyDate2 d= new MyDate2(sc.nextInt(),sc.nextInt(),sc.nextInt());         if(d.isCorrect){        System.out.println("If you want to konw yestoday date,please input -1 ");        System.out.println("or tomorrow date,please input 1");        System.out.println(d.dayAfter(sc.nextInt()));        System.out.println("Week : "+d.getWeek()+" "+d.toWeekString());        System.out.println("Input a cmparative date : ");        MyDate2 r=new MyDate2(sc.nextInt(),sc.nextInt(),sc.nextInt());        System.out.println(d.before(r));        System.out.println("Date_d between Date_r have "+d.daysBetween(r)+" days");        }        System.out.println();   System.out.println("Please input date : ");    }    }}




  

java之 ------ 類的封裝、繼承和多態(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.