用java逐步实现封装并打印给定月的日历

封装(Encapsulation)的简介 :是面向对象方法的重要原则,就是把对象的属性和操作(或服务)结合为一个独立的整体,并尽可能隐藏对象的内部实现细节。

封装是把过程和数据包围起来,对数据的访问只能通过已定义的接口。面向对象计算始于这个基本概念,即现实世界可以被描绘成一系列完全自治、封装的对象,这些对象通过一个受保护的接口访问其他对象。封装是一种信息隐藏技术,在java中通过关键字private,protected和public实现封装。什么是封装?封装把对象的所有组成部分组合在一起,封装定义程序如何引用对象的数据,封装实际上使用方法将类的数据隐藏起来,控制用户对类的修改和访问数据的程度。 适当的封装可以让程式码更容易理解和维护,也加强了程式码的安全性。

一、属性和构造方法

        定义三个属性:年、月、日

    /**
     * 年份
     */
    private int year;
    /**
     * 月份
     */
    private int month;
    /**
     * 天
     */
    private int day;

    public CalendarDate() {
    }

    /**
     * 构造方法 特点
     * 用来 创建 类的 对象 可以 给 类的属性赋值
     */
    public CalendarDate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

二、封装以下方法

        封装 getWeak() 方法

        作用:给定日期,获取该日期是星期几

    /**
     * 获取星期的方法
     * @return
     */
    public int getWeak() {
        int M = this.month >= 3 ? this.month - 2 : this.month + 10;
        int D = this.day;
        int year = this.month >= 3 ? this.year : this.year - 1;
        int Y = year % 100;
        int C = year / 100;

        int F = (26 * M - 2) / 10 + D + Y + Y / 4 + C / 4 - 2 * C;
        int W = F % 7;
        return W < 0 ? W + 7 : W;
    }

        封装 isLeap()  方法        

        作用:判断 该年 是否是闰年

    /**
     * 判断 当前年份 是否是闰年
     * @return
     */
    public boolean isLeap() {
        return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
    }

        封装 maxDay() 方法

        作用:计算当前月的最大值

    /**
     * 计算 当前月的 最大天数
     *
     * @param month
     * @return
     */
    public int maxDay(int month) {
        return switch (month) {
            case 1, 3, 5, 7, 8, 10, 12 -> 31;
            case 2 -> isLeap() ? 29 : 28;
            default -> 30;
        };
    }

        封装 showCalendar() 方法

        作用:打印该月的日历

    /**
     * showCalendar ()方法,用来显示当前月的日历
     */

    public void showCalendar() {
        //判断 当前 月份 1号 是星期几
        CalendarDate date = new CalendarDate(this.year, this.month, 1);
        int week = date.getWeak();
        int num = week == 0 ? 6 : week - 1;
        //打印 一二三四五六日
        System.out.println("一\t二\t三\t四\t五\t六\t日");
        //打印 1号前 的 空格
        for (int i = 0; i < num; i++) {
            System.out.print("\t");
        }
        //打印 日历
        for (int i = 1; i <= maxDay(this.month); i++) {
            if (i < 10) System.out.print(" ");
            System.out.print(i + "\t");
            //空格数 和 日期 满 7 换行
            if ((i + num) % 7 == 0) {
                System.out.println();
            }
        }
    }

三、调用封装的方法

package com.calendardate;

public class TextCase {
    public static void main(String[] args) {
        CalendarDate ca = new CalendarDate(2022, 6, 14);
        ca.showCalendar();
    }
}

四、运行结果

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值