【JavaSE】类和对象代码练习(年月日时分秒 简单时钟实现)

目录结构

在这里插入图片描述

最终效果

在这里插入图片描述

实现代码

MyData.java

package Data_Time;

/**
 * @ClassName MyData
 * @Description :TODO
 * @Author Josvin
 * @Date 2021/01/24/14:58
 */

/**
 * 构造方法:
 * MyDate(MyDate date);
 * MyDate(int year, int month, int day);
 *
 * 对方方法:
 * public void next();      //  让日期往后走一天
 * public void previous();  // 让日期往前走一天
 *
 * public String toString();    // 返回日期的字符串表示形式 年-月-日
 */

public class MyData {
    
    
    private int year;
    private int month;
    private int day;

    public MyData(MyData date) {
    
    
        this.year = date.year;
        this.month = date.month;
        this.day = date.day;
    }

    public MyData(int year, int month, int day) {
    
    
        check(year, month, day);
        // 代码可以运行到这个位置,说明方法内部没有抛异常
        // 所以参数是合法的

        this.year = year;
        this.month = month;
        this.day = day;
    }

    @Override
    public String toString() {
    
    
        return String.format("%d-%02d-%02d", year, month, day);
    }

    // 日期向后走一天
    public void next() {
    
    
        day++;
        if (day <= getDayOfMonth(year, month)) {
    
    
            return;
        }

        month++;
        day = 1;
        if (month <= 12) {
    
    
            return;
        }

        year++;
        month = 1;
    }

    // 日期向前走一天
    public void previous() {
    
    
        day--;
        if (day >= 1) {
    
    
            return;
        }

        month--;
        if (month >= 1) {
    
    
            day = getDayOfMonth(year, month);   // 必须先对 month--,再计算有多少天
            return;
        }

        year--;
        month = 12;
        day = getDayOfMonth(year, month);
    }

    private static void check(int year, int month, int day) {
    
    
        if (month < 1 || month > 12) {
    
    
            throw new RuntimeException("month 的有效范围是 [1, 12]");
        }

        int days = getDayOfMonth(year, month);
        if (day < 1 || day > days) {
    
    
            throw new RuntimeException("day 的有效范围是 [1, " + days + "]");
        }
    }

    private static final int[] DAYS = {
    
    31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    private static int getDayOfMonth(int year, int month) {
    
    
        if (month == 2 && isLeapYear(year)) {
    
    
            return 29;
        }

        return DAYS[month - 1];
    }

    private static boolean isLeapYear(int year) {
    
    
        return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
    }
}

MyTime.java

package Data_Time;

/**
 * @ClassName MyTime
 * @Description :TODO
 * @Author Josvin
 * @Date 2021/01/24/15:00
 */
public class MyTime {
    
    
    private int hour;
    private int minute;
    private int second;

    public MyTime(MyTime time) {
    
    
        this.hour = time.hour;
        this.minute = time.minute;
        this.second = time.second;
    }

    public MyTime(int hour, int minute, int second) {
    
    
        check(hour, minute, second);
        this.hour = hour;
        this.minute = minute;
        this.second = second;
    }

    // 返回值代表本次 Time 向后走一秒,是否走到第二天
    // true:有进位
    // false:没有进位
    public boolean next() {
    
    
        second++;
        if (second < 60) {
    
    
            return false;
        }

        minute++;
        second = 0;
        if (minute < 60) {
    
    
            return false;
        }

        hour++;
        minute = 0;

        if (hour < 24) {
    
    
            return false;
        }

        hour = 0;
        return true;
    }

    public boolean previous() {
    
    
        second--;
        if (second >= 0) {
    
    
            return false;
        }

        minute--;
        second = 59;
        if (minute >= 0) {
    
    
            return false;
        }

        hour--;
        minute = 59;
        if (hour >= 0) {
    
    
            return false;
        }

        hour = 23;
        return true;
    }

    @Override
    public String toString() {
    
    
        return String.format("%02d:%02d:%02d", hour, minute, second);
    }

    private static void check(int hour, int minute, int second) {
    
    
        if (hour < 0 || hour >= 24) {
    
    
            throw new RuntimeException("hour 的有效范围是 [0, 23]");
        }

        if (minute < 0 || minute >= 60) {
    
    
            throw new RuntimeException("minute 的有效范围 [0, 59]");
        }

        if (second < 0 || second >= 60) {
    
    
            throw new RuntimeException("second 的有效范围是 [0, 59]");
        }
    }
}

MyDateTime.java

package Data_Time;

/**
 * @ClassName MyDateTime
 * @Description :TODO
 * @Author Josvin
 * @Date 2021/01/24/15:12
 */

// 既有年月日,又有时分秒
public class MyDateTime {
    
    
    //    private int year;
//    private int month;
//    private int day;
    private MyData date;

    //    private int hour;
//    private int minute;
//    private int second;
    private MyTime time;

    public MyDateTime(MyDateTime datetime) {
    
    
        this.date = datetime.date;     // TODO: BUG fix
        this.time = datetime.time;   /*  // TODO: BUG fix*/

        /*this.date = new MyData(datetime.date);
        this.time = new MyTime(datetime.time);
*/
    }

    public MyDateTime(int year, int month, int day, int hour, int minute, int second) {
    
    
        this.date = new MyData(year, month, day);
        this.time = new MyTime(hour, minute, second);
    }

    // 部分构造,只传年月日,时分秒默认设置成 0时0分0秒
    public MyDateTime(int year, int month, int day) {
    
    
        this(year, month, day, 0, 0, 0);
    }

    public MyDateTime(MyData date) {
    
    
       this.date = date;   // TODO: BUG fix
        // this.date = new MyData(date);
        this.time = new MyTime(0, 0, 0);
    }

    //  向后走一秒
    public void next() {
    
    
        if (time.next()) {
    
    
            date.next();
        }
    }

    // 向前走一秒
    public void previous() {
    
    
        if (time.previous()) {
    
    
            date.previous();
        }
    }

    @Override
    public String toString() {
    
    
        return String.format("%s %s", date, time);
    }
}


Test11.java

package Data_Time; /**
 * @ClassName Data_Time.Test11
 * @Description :TODO
 * @Author Josvin
 * @Date 2021/01/24/15:16
 */


import java.util.concurrent.TimeUnit;

public class Test11 {
    
    
    public static void main(String[] args) throws InterruptedException {
    
    
        MyDateTime datetime = new MyDateTime(2021, 1, 31, 23, 59, 53);
        MyDateTime datetime2 = new MyDateTime(datetime);
/*

        datetime.next();
        datetime.next();
        System.out.println(datetime);
        System.out.println(datetime2);
*/

        while (true) {
    
    
            System.out.println(datetime);
            datetime.next();
            TimeUnit.SECONDS.sleep(1);
        }
    }
}


但是这里边有一个问题:
如果执行这段代码
在这里插入图片描述
这里输出了相同的结果,说明是有问题的。主要问题还是在这里,因为两个对象操作了同一个内容。
在这里插入图片描述
在这里插入图片描述
而我们应该操作不同的内容因该是这样两个部分:
在这里插入图片描述
修改之后:
在这里插入图片描述
在这里插入图片描述
我们再来运行Test11
在这里插入图片描述
操作的不在是同一块内容,所以dt2是没有变化的。

猜你喜欢

转载自blog.csdn.net/weixin_45532227/article/details/113109369
今日推荐