liuboris.slan 2021-10-04 19:59 采纳率: 100%
浏览 62
已结题

关于#java#的封装问题,请各位专家解答!


public class MyDate
{
    private int year, month, day;
    private static int thisYear;
    static 
    {
        thisYear = 2018;
    }

    public MyDate(int year, int month, int day) 
    {
        this.set(year, month, day);
    }

    public MyDate() 
    {
        this(1970, 1, 1);
    }

    public MyDate(MyDate date) 
    {
        this.set(date);
    }

    public 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;

    }

    public void set(MyDate date) 
    {
        this.set(date.year,date.month,date.day);
    }

    public int getYear() 
    {
        return this.year;
    }

    public int getMonth() 
    {
        return this.month;
    }

    public int getDay() 
    {
        return this.day;
    }

    public String toString() 
    {
        return year + "年" + String.format("%02d", month) + "月" + String.format("%02d", 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(MyDate date) 
    {
        return this == date
                || date != null && this.year == date.year && this.month == date.month && this.day == date.day;

    }

    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 MyDate.isLeapYear(year) ? 29 : 28;
        default:
            return 0;

        }
    }

    public int daysOfMonth() 
    {
        return daysOfMonth(this.year, this.month);
    }

    public void tomorrow() 
    {
        MyDate date = new MyDate() ;
        this.day = this.day%this.daysOfMonth() + 1;
        if (this.day == 1) 
        {
            this.month = this.month%12 + 1;
            if(this.month==1) {
                date.year++;
            }
                
        }

    }

    public MyDate yesterday() 
    {
        MyDate date = new MyDate(this);
        date.day--;
        if (date.day == 0) 
        {
            date.month = (date.month-2+12)%12+1;
            if (date.month == 12) 
                date.year--;
            date.day =  daysOfMonth(date.year,date.month);
            
        }
        return date;
    }

}

class MyDate_ex 
{
    public static void main(String[] args) 
    {
        System.out.println("今年是" + MyDate.getThisYear() + ", 闰年?" + MyDate.isLeapYear(MyDate.getThisYear()));
        MyDate d1 = new MyDate(2017, 12, 31);
        System.out.println(d1.getYear() + "年,闰年?" + d1.isLeapYear());
        MyDate d2 = new MyDate(d1);
        System.out
                .println("d1: " + d1 + ", d2: " + d2 + ", d1==d2? " + (d1 == d2) + ", d1.equals(d2)? " + d1.equals(d2));
        System.out.println(d1 + "的明天是 " );
        d1.tomorrow();
        System.out.println(d1 + "\n" + d2 + "的昨天是" + (d2 = d1.yesterday()));
    }
}


  • 写回答

4条回答 默认 最新

  • CSDN专家-link 2021-10-04 20:01
    关注

    你倒是说一下有啥问题啊

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 10月12日
  • 已采纳回答 10月4日
  • 创建了问题 10月4日

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序